gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:58:57 24 4
gpt4 key购买 nike

有这样一个字符串:

$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) 但不知道如果值在引号之间如何转义。任何人都可以告诉我如何并请解释正则表达式。谢谢!

最佳答案

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

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

DEMO

关于php - 按空格和冒号拆分字符串,但如果在引号内则不拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33041330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com