将字符串分解为标记,保持引用的substr完整

mad*_*php 6 php regex

我不知道我在哪里看到它,但任何人都可以告诉我如何使用PHP和正则表达式完成这个?

'this is a string "that has quoted text" inside.'
Run Code Online (Sandbox Code Playgroud)

我希望能够像这样爆炸它

[0]this
[1]is
[2]a
[3]string
[4]"that has quoted text"
[5]inside
Run Code Online (Sandbox Code Playgroud)

保持报价完整.

anu*_*ava 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."
}
Run Code Online (Sandbox Code Playgroud)

这是拨号盘上上述工作代码的链接

更新:要逃避支持,请尝试:

preg_split('#\s*((?<!\\\\)"[^"]*")\s*|\s+#', $str, -1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
Run Code Online (Sandbox Code Playgroud)