Search code examples
phpregexbbcode

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);

Solution

  • proper code should be:

    $replace = array('<b>$1</b>');
    
    return preg_replace($search, $replace, $input);