$regex = '/\[b\](.*?)\[\/b\]/is';
$string = '[b][b][b]string[/b][/b][/b]';
重复匹配这将只匹配直到第一个[/ B],因此,如果我用这个正则表达式来此的bbcode转换为HTML我将结束与此:内匹配
string[/b][/b]
我m使用PHP preg_replace,我如何才能最终得到string
,所以有3个html bold标签。
$regex = '/\[b\](.*?)\[\/b\]/is';
$string = '[b][b][b]string[/b][/b][/b]';
重复匹配这将只匹配直到第一个[/ B],因此,如果我用这个正则表达式来此的bbcode转换为HTML我将结束与此:内匹配
string[/b][/b]
我m使用PHP preg_replace,我如何才能最终得到string
,所以有3个html bold标签。
对于这样脏的情况:
this [b]is [b]a[/b][/b] test [b]string[/b]
递归解决方案的工作:
\[b](?:(?:(?!\[b]).)*?|(?R))*\[/b]
PHP代码:
$str = 'this [b]is [b]a[/b][/b] test [b]string[/b]';
echo preg_replace_callback('~\[(\w+)](?:(?:(?!\[\1]).)*?|(?R))*\[/(\1)]~', function($m) {
return "**".preg_replace("~\[/?$m[1]]~", '', $m[0])."**";
}, $str);
输出:
this **is a** test **string**
也许'\并[b \(\ /)〕''与'<$1strong>此不检查匹配配对虽然..这将与str_replace(array('[b]','[/ b]'),数组('','')' – chris85
一样吗? [(\/| | b \]'到'<$1b>')? – horcrux
您可以重复替换,直到没有任何其他内容匹配。 – apokryfos