1

是否可以匹配可能以随机序列出现的两个单词?例子:

$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 完成?

4

2 回答 2

2

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

'/couple.*2 pcs|2 pcs.*couple/' 
于 2013-02-27T19:42:02.573 回答
1

使用 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";
}
于 2013-02-27T19:28:17.653 回答