Sergio Toledo Piza
Sergio Toledo Piza

Reputation: 788

SyntaxHighlighter BBCode PHP

I'm having some problems with the BBCode I created to use with the SyntaxHighlighter

function bb_parse_code($str) {
    while (preg_match_all('`\[(code)=?(.*?)\]([\s\S]*)\[/code\]`', $str, $matches)) foreach ($matches[0] as $key => $match) {
        list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]); 
        switch ($tag) {
            case 'code': $replacement = '<pre class="brush: '.$param.'">'.str_replace("    ", " ", str_replace(array("<br>", "<br />"), "\n", $innertext))."</pre>"; break;

        }
        $str = str_replace($match, $replacement, $str);
    }
    return $str;
}

And I have the bbcode:

[b]bold[/b]
[u]underlined[/u]
[code=js]function (lol) {
alert(lol);
}[/code]
[b]bold2[/b]
[code=php]
<? echo 'lol' ?>
[/code]

Which returns this: BBCode

I know the problem is on the ([\s\S]*) of the regex that allows any character, but how do to make the code work with line breaks?

Upvotes: 1

Views: 369

Answers (1)

cmbuckley
cmbuckley

Reputation: 42468

You should use the following pattern:

`\[(code)=?(.*?)\](.*?)\[/code\]`s

A couple of changes:

Upvotes: 1

Related Questions