您可以使用
preg_match_all(...):
$text = 'Lorem ipsum "dolor sit amet" consectetur "adipiscing \"elit" dolor';preg_match_all('/"(?:\\.|[^\\"])*"|S+/', $text, $matches);print_r($matches);会产生:
Array( [0] => Array ( [0] => Lorem [1] => ipsum [2] => "dolor sit amet" [3] => consectetur [4] => "adipiscing "elit" [5] => dolor ))
如您所见,它还考虑了带引号的字符串中的转义引号。
编辑
简短说明:
"# match the character '"'(?: # start non-capture group 1 \ # match the character '' . # match any character except line breaks | # OR [^\"] # match any character except '' and '"')* # end non-capture group 1 and repeat it zero or more times"# match the character '"'|# ORS+ # match a non-whitespace character: [^s] and repeat it one or more times
并且在匹配
%22而不是双引号的情况下,您可以执行以下操作:
preg_match_all('/%22(?:\\.|(?!%22).)*%22|S+/', $text, $matches);


