有这样一个字符串:
$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);
是:
[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)
但不知道如果值在引号之间如何转义。任何人都可以告诉我如何并请解释正则表达式。谢谢!
最佳答案
关于php - 按空格和冒号拆分字符串,但如果在引号内则不拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041330/