0

只是缝合了一个小回调来突出显示我所有的 BBCode。花了我很多时间,因为正则表达式对我来说仍然是一个巨大的痛苦。

function highlight($str) {
  return '<b>'.$str[0].'</b>';
}

$str = '[b]Hello, World![/b] in either the color [blue]test[/blue] or [red]test[/red]';
$highlight = preg_replace_callback('|[[\/\!]*?[^\[\]]*?]|si', 'highlight', $str);
echo $highlight;

但是现在我真的很想做相反的事情:) 什么是正则表达式来突出显示除 BBCodes 之外的所有内容?

4

1 回答 1

0

这不是最好的解决方案,但它会起作用。

$re = '/
    (.*?)                           # text before bBB...eBB
        (\[(\w+?)\].*?\[\s*\/\3\])  # bBB...eBB
    |
    (.*?$)                          # text after last bBB..eBB
    /xui';

$string = "beginOfS [b]Hello, World![/b] in either the color [blue]test lorem [yel]ipsum[/yel] dolorem [/blue] or [red]test[/red] endOfS";

echo  preg_replace($re, '<b>\1\4</b>\2', $string);

// $nMatches = preg_match_all($re, $string, $aMatches);

返回:

<b>beginOfS </b>[b]Hello, World![/b]<b> in either the color </b>[blue]test lorem [yel]ipsum[/yel] dolorem [/blue]<b> or </b>[red]test[/red]<b> endOfS</b>
于 2011-04-11T14:18:20.890 回答