(first time poster, long time visitor via Google)
I am trying to extract the contents of some square brackets, but I have a problem. I have work for parentheses, as shown below, but I do not see how this should be changed to work in square brackets. I would think that replacing a square with a square and vice versa should work in this example, but apparently not.
It should ignore the brackets in parentheses. Therefore, he will not return (11), but will return (10 (11) 12).
$preg = '#\(((?>[^()]+)|(?R))*\)#x';
$str = '123(456)(789)(10(11)12)';
if(preg_match_all($preg, $str, $matches)) {
$matches = $matches[0];
} else {
$matches = array();
}
echo '<pre>'.print_r($matches,true).'</pre>';
This returns:
Array (
[0] => (456)
[1] => (789)
[2] => (10(11)12)
)
It's fine. However, how can I get this to work for a string with square brackets instead of eg:
$str = '123[456][789][10[11]12]';