출처 : https://sir.kr/pg_lecture/716
출처2 : http://www.gethifi.com/tools/regex
$sStr = '(1(2(3)(4(5)4)2)1)';
$i = 0;
while (preg_match("/\([^()]*\)/", $sStr, $aMatches)) {
$aRe[$i] = $aMatches[0];
$sStr = str_replace($aMatches[0], '^!__R_'.$i.'__!^', $sStr);
++$i;
}
var_dump($aRe, $sStr);
// 결과는 아래와 같음
array
0 => string '(3)' (length=3)
1 => string '(5)' (length=3)
2 => string '(44)' (length=4)
3 => string '(22)' (length=4)
4 => string '(11)' (length=4)
string '' (length=0)