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 に答える