0

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.

1
  • as an aside, never use double quoted strings where single quoted ones will do, especially in regex patterns Commented Jan 21, 2011 at 13:35

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

@ircmaxell: I'm trying to defy the constant connection reset errors that I'm actually getting right now, and showing my wi-fi router who's boss.
0

Try \[[a-z\s]+\] It will capture brackets and all contents

2 Comments

What would happen if you tried to match that against foo[bar]baz[biz buz]?
make it ungreedy \\[.*?\\] or just go with preg_replace('/\\[(?>[^\\]]*)\\]/', '', $content); less readable but the once-only subpattern makes it more efficient

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.