如何在preg_split
中合并两个定界符?例如:
$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
)
实际上,当两个定界符是邻居时,我想合并数组元素。换句话说,如果下一部分是第二个定界符,则忽略第一个定界符。
最佳答案
尝试使用字符类:/[ ,]+/
+
是一个量词,表示“1或更多”