这是我的代码:
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("[readmore]","",$string);
这是我的预期输出:
Hello this is my text and this is remaining text
这是我的实际输出:
Hello this is my text [] and this is remaining text
问题很简单,如何乘坐“[]”?
答案 0 :(得分:1)
你需要逃避[
& ]
。请尝试以下regexp,
preg_replace("/\[([^\[\]]++|(?R))*+\]/", "", $string);
<强>输出:强>
Hello this is my text and this is remaining text
答案 1 :(得分:0)
您需要转义括号以在正则表达式中使用。此外,您可能需要查看preg_replace
的手册,因为您已经离开了需要围绕正则表达式的/
。
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("/\[readmore\]/","",$string);
答案 2 :(得分:0)
转义'['和']'字符:
这个脚本:
$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("[\\[readmore\\] ]","",$string);
这个结果(我对此进行了测试):
Hello this is my text and this is remaining text
答案 3 :(得分:0)
使用str_replace
$string = "Hello this is my text [readmore] and this is remaining text";
echo str_replace("[readmore]","",$string);
<强>输出强>
Hello this is my text and this is remaining text