【发布时间】:2011-12-26 06:28:40
【问题描述】:
如何在 PHP 中使用空格来拆分字符串,同时尊重带引号的子字符串?我认为必须有一些命令来解析这种类型的字符串,如参数,但我不知道那是什么。
示例字符串:
$string = '12345 abcd "hello world" defgh "nice to meet you" 34554';
我通常使用这三种中的一种来分解字符串。但我不认为他们有能力像我建议的那样拆线。
$result = str_replace(' ',"\n",$string);
$result = explode(' ',$string);
$result = preg_replace('#\s#',"\n",$string);
结果:
12345
abcd
"hello
world"
defgh
"nice
to
meet
you"
34554
期望的结果:
12345
abcd
hello world
defgh
nice to meet you
34554
更新
我想我对 PHP 处理 URL 查询变量的 parse_str 命令印象深刻,并希望 PHP 有一些我可以在这个例子中工作的东西,有点像 getopt 对命令行参数所做的那样。
【问题讨论】:
标签: php regex string parsing arguments