【发布时间】:2016-01-13 05:47:03
【问题描述】:
假设我有以下字符串
$string1 = 'hello world my name is'
和
$string2 = '"hello world" my name is'
将它与 string1 一起使用:
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $string1, $matches);
我得到一个数组:
echo $matches[0][0]//hello
echo $matches[0][1] //world
使用相同但使用 string2:
preg_match_all('/"(?:\\\\.|[^\\\\"])*"|\S+/', $string2, $matches);
我得到一个数组:
echo $matches[0][0] //"hello world"
echo $matches[0][1] //my
但如果我的字符串是:
" hello world " my name is
//^^ ^^(notice the spaces at beginning and end ),
我会得到:
echo $matches[0][0] //" hello world "
当我真的很想得到时
"hello world"
如何修改preg_match_all中的第一个参数?还有其他简单的解决方案吗?谢谢
【问题讨论】:
-
为什么不能使用 preg_match_all('/hello world/', $string1, $matches);如果你只想要'hello world'