Php 正在分解没有引号的字符串
我有像abc xyz“a x”test“test1 test2”这样的字符串。我想用空格分割字符串,但引号内的单词应该保持原样。我可以使用explode拆分字符串,但explode不能按照我的要求在这里工作 分解/拆分后的输出应为Php 正在分解没有引号的字符串,php,Php,我有像abc xyz“a x”test“test1 test2”这样的字符串。我想用空格分割字符串,但引号内的单词应该保持原样。我可以使用explode拆分字符串,但explode不能按照我的要求在这里工作 分解/拆分后的输出应为 [0] => abc [1] => xyz [2] => "a x" [3] => test [4] => "test1 test2" 我认为preg_split适合我,但不知道正确的正则表达式。这样做: $str = 'this is
[0] => abc
[1] => xyz
[2] => "a x"
[3] => test
[4] => "test1 test2"
我认为preg_split适合我,但不知道正确的正则表达式。这样做:
$str = 'this is a string "that has quoted text" inside.';
// #1 version
$arOne = preg_split('#\s*("[^"]*")\s*|\s+#', $str, -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
// #2 version
$arTwo = preg_split('#\s*((?<!\\\\)"[^"]*")\s*|\s+#', $str, -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
摘自这里:另一个简单而好的答案是
$str = abc xyz "a x" test "test1 test2"
str_getcsv($str,' ','"');
将返回一个数组以获取有关
它将返回按空间分割的数组,不带“
以上产量
[0] => abc
[1] => xyz
[2] => a x
[3] => test
[4] => test1 test2
可能重复的
[0] => abc
[1] => xyz
[2] => a x
[3] => test
[4] => test1 test2