Anthony Anthony - 1 year ago 151
PHP Question

PHP String Replace for BBCode

I want to make a custom BBCode for my forum site, but I've run into an issue, and I'm having a hard time fixing it.

This is what's in the database for the body of the thread "[b]Bold[/b][i]Italic[/i][strike]Strike[/strike]".

However the output is displayed like this "[i]Italic[/i][strike]Strike[/strike]".

So, I'm guessing it's an issue with echoing it out, but i'm not sure how to fix it. Here's the current code:

function bbcode($input) {
$input = strip_tags($input);
$input = htmlentities($input);

$search = array('/\[b\](.*?)\[\/b\]/is');

$replace = array('<b>$body</b>');

return preg_replace($search, $preg_replace, $input);

while($row = mysql_fetch_array($threadquery, MYSQL_ASSOC)) {
$body = str_replace("\n",'<br>', $row['body']);

echo bbcode($body);

Answer Source

proper code should be:

$replace = array('<b>$1</b>');

return preg_replace($search, $replace, $input);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download