找到多个单词?

时间:2013-07-02 14:20:17

标签: php

您能告诉我如何使用strops查看字符串是否包含两个特定字词中的任何一个? (?if

$phrase = “how to use strops?”
if(strpos($phrase, "?|if") === false) //Is there a way I can detect the presence of
{               //either words in a sentence using one line of code that has strpos

     echo “neither word is found”;
}

2 个答案:

答案 0 :(得分:2)

strpos()不支持正则表达式。所以你要么使用正则表达式(比如preg_match(),要么做一些不太复杂的事情,如:

$phrase = "how to use strpos";
$words = array('foo', 'bar');

$matches = 0;
foreach( $words as $word ) {
   $matches += ( strpos( $phrase, $word ) !== false ) ? 1 : 0;
}

printf("%d words matches", $matches);

答案 1 :(得分:-2)

if ((strpos($phrase, "?")===FALSE)$$(strpos($phrase,"if")===FALSE)) echo "neither word is found";