PHP爆炸字符串,但将引号中的单词视为单个单词 [英] PHP explode the string, but treat words in quotes as a single word
本文介绍了PHP爆炸字符串,但将引号中的单词视为单个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何爆炸以下字符串:
Lorem ipsum "dolor sit amet" consectetur "adipiscing elit" dolor
进入
array("Lorem", "ipsum", "dolor sit amet", "consectetur", "adipiscing elit", "dolor")
以便将引号中的文本视为一个单词.
So that the text in quotation is treated as a single word.
这是我现在拥有的:
$mytext = "Lorem ipsum %22dolor sit amet%22 consectetur %22adipiscing elit%22 dolor"
$noquotes = str_replace("%22", "", $mytext");
$newarray = explode(" ", $noquotes);
但是我的代码将每个单词分成一个数组.如何使引号内的单词被视为一个单词?
but my code divides each word into an array. How do I make words inside quotation marks treated as one word?
推荐答案
您可以使用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
)
)
正如您所看到的,它也考虑了带引号的字符串中的转义引号.
And as you can see, it also accounts for escaped quotes inside quoted strings.
编辑
简短说明:
" # 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 '"'
| # OR
\S+ # match a non-whitespace character: [^\s] and repeat it one or more times
如果匹配%22
而不是双引号,则可以执行以下操作:
And in case of matching %22
instead of double quotes, you'd do:
preg_match_all('/%22(?:\\\\.|(?!%22).)*%22|\S+/', $text, $matches);
这篇关于PHP爆炸字符串,但将引号中的单词视为单个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文