正则表达式Preg_replace

时间:2012-05-30 18:05:18

标签: php regex preg-replace bbcode

我正在尝试使用preg_replace转换来bbcode。基本上,这个

[QUOTE=ksiva]blahblah blah[/QUOTE]

需要看起来像这样

<div class=quote-msg"><div class="quote-author"><em>ksiva</em></div>blahblah blah</div>

我试过这个preg替换,但它不起作用。我做错了什么?

$pattern = '#\[QUOTE=([a-zA-Z]*|\#?[0-9a-fA-F]{6})](.*?)\[/QUOTE\]#s';

$replace = '<div class="quote-msg"><div class="quote-author><em>$1</em></div>$2</div>';
$text = PREG_REPLACE($pattern, $replace, $text);

1 个答案:

答案 0 :(得分:1)

弹出这段代码,我认为这就是你要找的东西。

$pattern = '/\[QUOTE\=([^]]*)]([^\[]*)\[\/QUOTE]/';
$text = '[QUOTE=ksiva]blahblah blah[/QUOTE]';
$replace = "<div class=\"quote-msg\"><div class=\"quote-author\"><em>$1</em></div>$2</div>";
$text = PREG_REPLACE($pattern, $replace, $text);