小编Jua*_*los的帖子

使用正则表达式以任意顺序匹配多个单词

好吧,假设我想匹配一个句子中的 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",
);
Run Code Online (Sandbox Code Playgroud)

所以我需要匹配单词“二”,“四”和“十”,但按任何顺序,它们之间可以有也可以没有任何其他单词。我尝试

foreach($sentences AS $sentence) {
   $n++;
   if(preg_match("/(two)(.*)(four)(.*)(ten)/",$sentence)) {
       echo $n." matched\n";
   }
}
Run Code Online (Sandbox Code Playgroud)

但这只会匹配句子 1,我需要匹配句子 1、2、4 和 5。

我希望你能帮忙...问候!(抱歉我的英语)

php regex

6
推荐指数
1
解决办法
4796
查看次数

标签 统计

php ×1

regex ×1