我不知道我在哪里看到它,但是有人能告诉我如何使用php和regex完成这个吗?
'this is a string "that has quoted text" inside.'
我希望能够像这样爆炸它
[0]this
[1]is
[2]a
[3]string
[4]"that has quoted text"
[5]inside
保持报价不变。
答案 0 :(得分:3)
需要PHP> = 5.3.0
$str = 'this is a string "that has quoted text" inside';
$x = str_getcsv($str,' ','"');
var_dump($x);
这会删除引号,但确实会保留引用块的内容。
答案 1 :(得分:3)
您可以尝试以下代码:
$str = 'this is a string "that has quoted text" inside.';
var_dump ( preg_split('#\s*("[^"]*")\s*|\s+#', $str, -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) );
Output:
array(6) {
[0]=>
string(4) "this"
[1]=>
string(2) "is"
[2]=>
string(1) "a"
[3]=>
string(6) "string"
[4]=>
string(22) ""that has quoted text""
[5]=>
string(7) "inside."
}
以下是above working code on dialpad
的链接preg_split('#\s*((?<!\\\\)"[^"]*")\s*|\s+#', $str, -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
答案 2 :(得分:1)
这适用于regexpal.com,带有您的示例字符串:
((".*?")|([\S]*))
答案 3 :(得分:1)
如果您不一定需要正则表达式,也可以使用strtok
来标记字符串。有关示例,请参阅此tokenizedQuoted
function in the comments on the strtok
manual page和my enhancement of that tokenizedQuoted
function。
答案 4 :(得分:0)
这需要回顾并展望未来......
尝试类似:
preg_split('/(?<!(".+)) (?!(.+"))/', $str, -1 ,PREG_SPLIT_NO_EMPTY);
[未经测试]