如何合并preg_split
中的两个分隔符?例如:preg_split在PHP中有两个分隔符
$str = "this is a test , and more";
$array = preg_split('/(|,)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
print_r($array);
会产生一个数组作为
Array
(
[0] => this
[1] =>
[2] => is
[3] =>
[4] => a
[5] =>
[6] => test
[7] =>
[8] =>
[9] => ,
[10] =>
[11] =>
[12] => and
[13] =>
[14] => more
)
,但我想
Array
(
[0] => this
[1] =>
[2] => is
[3] =>
[4] => a
[5] =>
[6] => test
[7] => ,
[8] => and
[9] =>
[10] => more
)
其实,我想在两个分隔符是邻居合并数组元素。换句话说,如果下一个部分是第二个分隔符,则忽略第一个分隔符。
你的最后一段是不能用正则表达式来完成的。你需要为此定制代码。 –