preg_replace在php中删除在sting中多次出现的标记
我有一个问题,我需要清除变量中的所有字体标记:preg_replace在php中删除在sting中多次出现的标记,php,regex,Php,Regex,我有一个问题,我需要清除变量中的所有字体标记: $old = '[FONT=arial]Dit is een tekst [FONT=Courier New]nonononon'; $new = preg_replace('/\[FONT=(.*)\](.*)/', "$2", $old); echo "\nold = $old\nnew = $new\n"; 但其结果是: old = [FONT=arial]Dit is een tekst [FONT=Courier New]nonon
$old = '[FONT=arial]Dit is een tekst [FONT=Courier New]nonononon';
$new = preg_replace('/\[FONT=(.*)\](.*)/', "$2", $old);
echo "\nold = $old\nnew = $new\n";
但其结果是:
old = [FONT=arial]Dit is een tekst [FONT=Courier New]nonononon
new = nonononon
我想要的结果是:
old = [FONT=arial]Dit is een tekst[FONT=Courier New]nonononon
new = Dit is een tekst nonononon
您需要使正则表达式变为惰性,请使用
(.*)
参考:-
输出
Dit is een tekst nonononon
你不需要任何捕获组。您可以使用此正则表达式匹配并删除不需要的内容:
$old = '[FONT=arial]Dit is een tekst [FONT=Courier New]nonononon';
$new = preg_replace('/\[FONT=[^]]*]/', '', $old);
echo "$new\n";
[^]]
是一个否定字符类,它将匹配任何非]
字符
Dit is een tekst nonononon
$old = '[FONT=arial]Dit is een tekst [FONT=Courier New]nonononon';
$new = preg_replace('/\[FONT=[^]]*]/', '', $old);
echo "$new\n";
Dit is een tekst nonononon