有一个类似这样的字符串:
$str = "dateto:'2015-10-07 15:05' xxxx datefrom:'2015-10-09 15:05' yyyy asdf"
期望的结果是:
[0] => Array (
[0] => dateto:'2015-10-07 15:05'
[1] => xxxx
[2] => datefrom:'2015-10-09 15:05'
[3] => yyyy
[4] => asdf
)
我能得到什么:
preg_match_all("/\'(?:[^()]|(?R))+\'|'[^']*'|[^(),\s]+/", $str, $m);
is:
[0] => Array (
[0] => dateto:'2015-10-07
[1] => 15:05'
[2] => xxxx
[3] => datefrom:'2015-10-09
[4] => 15:05'
[5] => yyyy
[6] => asdf
)
我也尝试了 preg_split("/[\s]+/", $str)
,但不知道如何在值被引号包围时进行转义。请问有人可以展示并解释一下这个正则表达式吗?谢谢!
"~'[^']*'(*SKIP)(*F)|\s+~"
吗?我只能理解部分内容,我想要完全了解它。 - caramba'[^']*'
匹配所有单引号块和 (*SKIP)(*F) 使匹配失败。而|\s+
则匹配其余的所有空格。 - Avinash Raj