4

My PHP script calls the Freebase API and outputs a string that can contain any number of open then closed brackets. Each set of open then closed brackets can also contain any number of open then closed brackets itself. For example;

$string = "random string blah (a) (b) blah blah (brackets (within) brackets) blah";

How can I use PHP and regex to manipulate the string resulting in an output that doesn't contain the contents of any brackets or the brackets themselves? For example;

$string = "random string blah blah blah blah";
Callum Whyte
  • 2,379
  • 11
  • 36
  • 55

1 Answers1

9

You can use a recursive regex:

$result = preg_replace('/\(([^()]*+|(?R))*\)\s*/', '', $subject);

Explanation:

\(       # Match (
(        # Match the following group:
 [^()]*+ # Either any number of non-parentheses (possessive match)
|        # or
 (?R)    # a (recursive) match of the current regex
)*       # Repeat as needed
\)       # Match )
\s*      # Match optional trailing whitespace
Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • This is an excellent answer! One thing to watch out for is that your end result will have a couple of extra spaces in it that you may not want. For instance, using this regex for your test string will yield `random_string_blah___blah_blah__blah` instead of `random_string_blah_blah_blah_blah`, so make sure to remove those extra spaces if necessary. (Replaced spaces with underscores because spaces weren't preserved in the comments). – Nick Coons Aug 10 '13 at 17:50
  • @NickCoons: Good point. I've added a whitespace remover :). Protip: Type `Alt` + (`Keypad 0` `Keypad 1` `Keypad 6` `Keypad 0`) to create non-breakable spaces. – Tim Pietzcker Aug 10 '13 at 17:53
  • @TimPietzcker: Thanks for the tip. My numeric keypad is broken on this keyboard, so I'll have to try this from a different system. – Nick Coons Aug 10 '13 at 17:56