在PHP中,你可以使用正则表达式和preg_replace函数来实现爆破逗号但忽略括号内的逗号。下面是一个示例代码:
function explodeComma($string) {
$pattern = '/,(?![^(]*\))/';
$replacement = '|';
$result = preg_replace($pattern, $replacement, $string);
return explode('|', $result);
}
// 测试示例
$string = 'a, b, (c, d), e, f, (g, h, i), j';
$result = explodeComma($string);
print_r($result);
输出结果:
Array
(
[0] => a
[1] => b
[2] => (c, d)
[3] => e
[4] => f
[5] => (g, h, i)
[6] => j
)
在上面的示例中,我们定义了一个explodeComma
函数,它接受一个包含逗号的字符串作为参数。通过使用正则表达式/(?![^(]*\))/
,我们匹配逗号但忽略了括号内的逗号。然后,我们使用preg_replace
函数将匹配的逗号替换为|
。最后,我们使用explode
函数将字符串按照|
拆分成数组。
这样,我们就可以得到一个爆破逗号但忽略括号内的逗号的数组。
上一篇:爆破不一致的结果