Php preg替换多个结果
我目前正在用PHP编写一个函数,为论坛引擎翻译BBCodes。 现在我想添加一个Php preg替换多个结果,php,preg-replace,bbcode,Php,Preg Replace,Bbcode,我目前正在用PHP编写一个函数,为论坛引擎翻译BBCodes。 现在我想添加一个[code]-标记,并创建了以下函数: $txt = preg_replace('#\[code\](.*)\[(.*)\[/code\]#isU', "<div class=\"bb_uncode\">$1[$2</div>", $txt); $txt=preg\u replace('\[code\](.*)\[(.*)\[/code\]\isU',“$1&\$91;$2
[code]
-标记,并创建了以下函数:
$txt = preg_replace('#\[code\](.*)\[(.*)\[/code\]#isU', "<div class=\"bb_uncode\">$1[$2</div>", $txt);
$txt=preg\u replace('\[code\](.*)\[(.*)\[/code\]\isU',“$1&\$91;$2”,$txt);
(旁注:和#91;
等于[)如果[code]-标记中只有一个
[
,则此操作非常有效,但它会忽略每一个后续标记。是否有可能每隔一个括号也应用此搜索模式?使用
preg\u replace\u callback()
执行此操作:
$txt=preg\u replace\u回调('\[code\](.*)\[/code\]\isU',函数($match){
返回“”。
str#u replace(“[”,“[;”,$match[1])。
"");
},$txt);
使用preg\u replace\u callback()执行此操作。
:
$txt=preg\u replace\u回调('\[code\](.*)\[/code\]\isU',函数($match){
返回“”。
str#u replace(“[”,“[;”,$match[1])。
"");
},$txt);
您只能使用preg\u replace执行此操作:
$txt=preg\u replace('~(?>\[code]|\G(?您只能使用preg\u replace执行此操作:
$txt=preg\u replace('~(?>\[code]\\G(?)?
$txt = preg_replace_callback('#\[code\](.*)\[/code\]#isU', function($match) {
return "<div class=\"bb_uncode\">" .
str_replace('[', '[', $match[1]) .
"</div>");
}, $txt);
$txt = preg_replace('~(?>\[code]|\G(?<!^))[^[]*+\K\[(?!/code])~i',
'[', $txt);
(?> # open a non capturing group (atomic *)
\[code] # [code]
| # OR
\G # contiguous to the last match
(?<!^) # and not preceded by the begining of the string
) # close the non capturing group
[^[]*+ # 0 or more characters that are not a [ (possessive *)
\K # reset all that have been matched before
\[ # a literal [
(?!/code]) # not followed by /code]