按空格和冒号拆分字符串,但如果在引号内则不拆分

car*_*mba 5 php regex preg-match-all preg-match preg-split

有一个这样的字符串:

$str = "dateto:'2015-10-07 15:05' xxxx datefrom:'2015-10-09 15:05' yyyy asdf"
Run Code Online (Sandbox Code Playgroud)

想要的结果是:

[0] => Array (
    [0] => dateto:'2015-10-07 15:05'
    [1] => xxxx
    [2] => datefrom:'2015-10-09 15:05'
    [3] => yyyy
    [4] => asdf
)
Run Code Online (Sandbox Code Playgroud)

我得到了什么:

preg_match_all("/\'(?:[^()]|(?R))+\'|'[^']*'|[^(),\s]+/", $str, $m);
Run Code Online (Sandbox Code Playgroud)

是:

[0] => Array (
    [0] => dateto:'2015-10-07
    [1] => 15:05'
    [2] => xxxx
    [3] => datefrom:'2015-10-09
    [4] => 15:05'
    [5] => yyyy
    [6] => asdf
)
Run Code Online (Sandbox Code Playgroud)

preg_split("/[\s]+/", $str)如果值在引号之间,也尝试过但不知道如何转义。谁能告诉我如何并请解释正则表达式。谢谢!

Avi*_*Raj 5

我会使用 PCRE 动词(*SKIP)(*F)

preg_split("~'[^']*'(*SKIP)(*F)|\s+~", $str);
Run Code Online (Sandbox Code Playgroud)

演示