PHP preg_match,当两个单词可能以随机顺序出现时进行匹配


php preg_match, matching when 2 words might come in random sequence

有可能匹配两个随机序列的单词吗?例子:

$title = "2 pcs watch for couple";
$title = "couple watch 2 pcs";

目前我使用两个正则表达式:

if (preg_match("/2( )?pcs/i", $title) and preg_match("/couple/i", $title))

只是想知道是否只需要1就可以完成?

如果您只是测试字符串中是否存在两个单词,您可以使用

'/couple.*2 pcs|2 pcs.*couple/' 

use strpos()

if(strpos($string, '2 pcs') !== False){
 //its found
}

或首尾匹配

if(preg_match("/(^(2 pcs|couples)|(2 pcs|couples)$)/", '2 pcs watch for couples')){
//echo "found";
}

匹配任何

:

if(preg_match("/(2 pcs|couples)/", '2 pcs watch for couples')){
//echo "found";
}