PHP: regular expression to remove bracket codes
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_rep开发者_开发百科lace("/\[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.
You escaped the []
, but didn't add a second set of unescaped []
to designate a character class. Also, the s
is not necessary if you're not using the .
metacharacter in your regex.
Try this:
/\[[a-z\s]+\]/i
If you don't care what's between the square brackets and just want to remove everything contained in them, this will do:
/\[[^]]+\]/i
Try \[[a-z\s]+\]
It will capture brackets and all contents
精彩评论