【发布时间】:2014-12-06 05:13:47
【问题描述】:
好吧,假设我想在一个句子中匹配 3 个单词...但我需要以任何顺序匹配它们,例如:
$sentences = Array(
"one two three four five six seven eight nine ten",
"ten nine eight seven six five four three two one",
"one three five seven nine ten",
"two four six eight ten",
"two ten four six one",
);
所以我需要匹配“二”、“四”和“十”这几个词,但顺序不限,它们之间可以或不可以有任何其他词。我试试
foreach($sentences AS $sentence) {
$n++;
if(preg_match("/(two)(.*)(four)(.*)(ten)/",$sentence)) {
echo $n." matched\n";
}
}
但这只会匹配句子 1,我需要匹配句子 1、2、4 和 5。
希望能帮到你... 问候! (对不起我的英语)
【问题讨论】:
-
try this... 不是用于 php,而是用于正则表达式...and actual documentation
-
也...你may not need regex这个...只需检查字符串是否包含其他字符串。 stackoverflow.com/questions/4366730/…