regex - PHP strip all brackets from string -


this question has answer here:

my php script calls freebase api , outputs string can contain number of open closed brackets. each set of open closed brackets can contain number of open closed brackets itself. example;

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

how can use php , regex manipulate string resulting in output doesn't contain contents of brackets or brackets themselves? example;

$string = "random string blah blah blah blah"; 

you can use recursive regex:

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

explanation:

\(       # match ( (        # match following group:  [^()]*+ # either number of non-parentheses (possessive match) |        # or  (?r)    # (recursive) match of current regex )*       # repeat needed \)       # match ) \s*      # match optional trailing whitespace 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

.htaccess - Matching full URL in RewriteCond -