Php 预匹配检查所有单词和条件
我已经编写了一个正则表达式,它在或条件中对搜索项进行排序,这样就提供了三个单词,它们在字符串中的顺序与否无关。现在我想把AND条件放在一起,因为我想把这三个单词同时放在一个顺序不同的字符串中 这是我的Php 预匹配检查所有单词和条件,php,regex,preg-match,Php,Regex,Preg Match,我已经编写了一个正则表达式,它在或条件中对搜索项进行排序,这样就提供了三个单词,它们在字符串中的顺序与否无关。现在我想把AND条件放在一起,因为我想把这三个单词同时放在一个顺序不同的字符串中 这是我的preg\u match()常规表达式 $myPregMatch = (preg_match("/\b^(Word1|Word2|Word3)\b/", "Word4 Word2 Word1 Word3 Word5 Word7")); if ($myPregMatch){ echo "F
preg\u match()
常规表达式
$myPregMatch = (preg_match("/\b^(Word1|Word2|Word3)\b/", "Word4 Word2 Word1 Word3 Word5 Word7"));
if ($myPregMatch){
echo "FOUND !!!";
}
我想在字符串
“Word4 Word2 Word1 Word3 Word5 Word7”
中找到所有单词的顺序都不同。如果示例字符串是“Word5 Word7 Word3 Word2”
,则它不应返回。您需要锚定外观标题:
^(?=.*\bWord1\b)(?=.*\bWord2\b)(?=.*\bWord3\b)
看
如果输入字符串中有换行符,则需要使用/s
修饰符
以下是一份:
结果:
找到了代码>检查每个单词可能会更快
$string = "Word4 Word2 Word1 Word3 Word5 Word7"; // input string
$what = "Word2 Word1 Word3"; // words to test
$words = explode(' ', $what); // Make array
$i = count($words);
while($i--)
if (false == strpos($string, $words[$i]))
break;
$result = ($i==-1); // if $i == -1 all words are present in input string
这是一个很好的示例演示字符串,但实际上,会有句号、逗号、分号等,如果没有正则表达式,整词搜索将变得不可能。
$string = "Word4 Word2 Word1 Word3 Word5 Word7"; // input string
$what = "Word2 Word1 Word3"; // words to test
$words = explode(' ', $what); // Make array
$i = count($words);
while($i--)
if (false == strpos($string, $words[$i]))
break;
$result = ($i==-1); // if $i == -1 all words are present in input string