我想检查一个字符串是否包含两个特定的单词。
例如:
我需要检查字符串是否包含 "MAN live in WAN"
这样的句子中的 "MAN"& "WAN"
,并且 返回 true else false
.
我尝试过以条件方式循环字符串函数:-
<?php
$data = array("MAN","WAN");
$checkExists = $this->checkInSentence($data);
function checkInSentence( $data ){
$response = TRUE;
foreach ($data as $value) {
if (strpos($string, $word) === FALSE) {
return FALSE;
}
}
return $response;
}
?>
这是正确的方法还是我必须改变它?如何实现这一点任何建议或想法都将受到高度赞赏。
Note: data array may contain more than two words may be. I just need check whether a set of words are exists in a paragraph or sentence.
提前致谢!
最佳答案
已经差不多好了。如果不包含某个单词,您需要将其响应设置为 false。现在它总是会给出 true。
if (strpos($string, $word) === FALSE) {
$response = FALSE;
}
关于php - 检查给定的两个单词是否存在于 PHP 字符串中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27840587/