PHP:正则表达式删除括号代码
我正在尝试创建一个函数来删除所有括号代码,但它似乎不起作用,
function anti_code($content)
{
# find the matches and then remove them
$output = preg_replace("/\[a-z\s+\]/is", "", $content);
# return the result
return $output;
}
我希望在输出中删除这些代码,
Agro[space]terrorism
Agro[en space]terrorism
这样我就可以知道
Agroterrorism
我的正则表达式一定有问题!请告诉我。谢谢。
I am trying to make a function to remove all the bracket codes but it doesn't seem to be working,
function anti_code($content)
{
# find the matches and then remove them
$output = preg_replace("/\[a-z\s+\]/is", "", $content);
# return the result
return $output;
}
I want these codes to be removed in the output,
Agro[space]terrorism
Agro[en space]terrorism
so that I can get
Agroterrorism
I must be something wrong in my regular expression! Please let me know. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您转义了
[]
,但没有添加第二组未转义的[]
来指定字符类。此外,如果您在正则表达式中不使用.
元字符,则不需要s
。试试这个:
如果您不关心方括号之间的内容而只想删除其中包含的所有内容,则可以这样做:
You escaped the
[]
, but didn't add a second set of unescaped[]
to designate a character class. Also, thes
is not necessary if you're not using the.
metacharacter in your regex.Try this:
If you don't care what's between the square brackets and just want to remove everything contained in them, this will do:
尝试
\[[az\s]+\]
它将捕获括号和所有内容Try
\[[a-z\s]+\]
It will capture brackets and all contents