Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php preg替换多个结果_Php_Preg Replace_Bbcode - Fatal编程技术网

Php preg替换多个结果

Php preg替换多个结果,php,preg-replace,bbcode,Php,Preg Replace,Bbcode,我目前正在用PHP编写一个函数,为论坛引擎翻译BBCodes。 现在我想添加一个[code]-标记,并创建了以下函数: $txt = preg_replace('#\[code\](.*)\[(.*)\[/code\]#isU', "<div class=\"bb_uncode\">$1&#91;$2</div>", $txt); $txt=preg\u replace('\[code\](.*)\[(.*)\[/code\]\isU',“$1&\$91;$2

我目前正在用PHP编写一个函数,为论坛引擎翻译BBCodes。 现在我想添加一个
[code]
-标记,并创建了以下函数:

$txt = preg_replace('#\[code\](.*)\[(.*)\[/code\]#isU', "<div class=\"bb_uncode\">$1&#91;$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('[', '&#91;', $match[1]) .
           "</div>"); 
}, $txt);
$txt = preg_replace('~(?>\[code]|\G(?<!^))[^[]*+\K\[(?!/code])~i',
                    '&#91;', $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]