【发布时间】:2012-12-19 00:40:02
【问题描述】:
如何合并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
)
其实我想在两个定界符相邻的情况下合并数组元素。换句话说,如果下一部分是第二个分隔符,则忽略第一个分隔符。
【问题讨论】:
-
您的最后一段是正则表达式无法完成的。为此,您需要自定义代码。
标签: php arrays split preg-split