Reputation: 788
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:
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
Reputation: 42468
You should use the following pattern:
`\[(code)=?(.*?)\](.*?)\[/code\]`s
A couple of changes:
.*?
to make the quantifier lazy.s
modifier at the end, which causes .
to match new lines too. Upvotes: 1