regex - PHP strip all brackets from string -
this question has answer here:
- remove text between parentheses php 7 answers
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
Post a Comment