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

这是我的实际输出:

Hello this is my text [] and this is remaining text

问题也很简单,如何获得“[]”?

4

4 回答 4

1

你需要逃避[& ]。试试下面的正则表达式,

preg_replace("/\[([^\[\]]++|(?R))*+\]/", "", $string);

输出:

Hello this is my text and this is remaining text

键盘演示。

于 2013-04-20T08:13:12.910 回答
0

您需要转义括号以在正则表达式中使用。此外,您可能需要查看手册preg_replace,因为您省略了/需要在您的正则表达式周围的内容。

$string = "Hello this is my text [readmore] and this is remaining text";
echo preg_replace("/\[readmore\]/","",$string);
于 2013-04-20T08:12:59.963 回答
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     
于 2013-04-20T08:13:10.497 回答
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
于 2013-04-20T08:20:45.770 回答