我需要用preg_split
生成一个数组,因为implode('', $array)
可以重新生成原始字符串。preg_split的
$str = 'this is a test "some quotations is her" and more';
$array = preg_split('/( |".*?")/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
生成
数组Array
(
[0] => this
[1] =>
[2] => is
[3] =>
[4] => a
[5] =>
[6] => test
[7] =>
[8] =>
[9] => "some quotations is here"
[10] =>
[11] =>
[12] => and
[13] =>
[14] => more
)
我也需要注意引号前后的空间,以生成具有原始字符串的精确模式的数组。
例如:字符串为test "some quotations is here"and
,则数组为
Array
(
[0] => test
[1] =>
[2] => "some quotations is here"
[3] => and
)
这样行吗?
preg_split('/( ?".*?" ?| )/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
应该可以了
$str = 'this is a test "some quotations is her" and more';
$result = preg_split('/(?:("[^"]+")|b)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE);
$result = array_slice($result, 1,-1);
输出Array
(
[0] => this
[1] =>
[2] => is
[3] =>
[4] => a
[5] =>
[6] => test
[7] =>
[8] => "some quotations is her"
[9] =>
[10] => and
[11] =>
[12] => more
)
重建implode('', $result);
// => this is a test "some quotations is her" and more