这个片段可以帮助我使用嵌套括号。基本上想法是用一些标识符递归替换 (*) 直到没有更多的括号。然后用逗号分解字符串,然后将所有内容放回原处。这不是理想的解决方案 - 刚刚在大约 30 分钟内完成,但它可以工作:) 它绝对可以以某种方式进行优化。
/**
* Explode string by delimiter, but don't explode if delimiter is inside parenthesis.
* This also support nested parenthesis - that's where pure RegExp solutions fails.
*
* For example,
* $input = "one, two three, four (five, (six, seven), (eight)) (nine, ten), eleven";
* $output = array(
* 'one',
* 'two three',
* 'four (five, (six, seven), (eight)) (nine, ten)',
* 'eleven'
* );
*
* @param string $input
* @param string $delimiter = ,
* @param string $open_tag = \(
* @param string $close_tag = \)
* @return array
*/
function exploder($input, $delimiter = ',', $open_tag = '\(', $close_tag = '\)')
{
// this will match any text inside parenthesis
// including parenthesis itself and without nested parenthesis
$regexp = '/'.$open_tag.'[^'.$open_tag.$close_tag.']*'.$close_tag.'/';
// put in placeholders like {{\d}}. They can be nested.
$r = array();
while (preg_match_all($regexp, $input, $matches)) {
if ($matches[0]) {
foreach ($matches[0] as $match) {
$r[] = $match;
$input = str_replace($match, '{{'.count($r).'}}', $input);
}
} else {
break;
}
}
$output = array_map('trim', explode($delimiter, $input));
// put everything back
foreach ($output as &$a) {
while (preg_match('/{{(\d+)}}/', $a, $matches)) {
$a = str_replace($matches[0], $r[$matches[1] - 1], $a);
}
}
return $output;
}
$a = "one, two three, four (five, (six, seven), (eight)) (nine, ten), eleven";
var_dump(exploder($a));
这将输出:
array (size=4)
0 => string 'one' (length=3)
1 => string 'two three' (length=9)
2 => string 'four (five, (six, seven), (eight)) (nine, ten)' (length=46)
3 => &string 'eleven' (length=6)
正如预期的那样。