how to check if some characters are in an string?
本问题已经有最佳答案,请猛点这里访问。
我们有一个类似的字符串:
1 | $string="Lorem Ipsum is simply dummy text of the printing and typesetting industry."; |
所以我想检查这个字符串中是否有可用的单词。例如,单词:
1 | $words=['Ipsum','54']; |
可以看到
一个简单的循环与
1 2 3 4 5 6 | function containsOneOfThoseWords($str, $words) { foreach ($words as $word) { if (strpos($str, $word) !== false) return true; } return false; } |
1 2 3 4 5 6 7 |
只需从上面的代码片段中创建一个函数。
更新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | $ cat search.php <?php $domain="asdasd asd fasdfasfafafad f454 asdfa"; $lookfor=array('as','f454'); function containsOneOfThoseWords($str, $words) { foreach ($words as $word) { if (strpos($str, $word) !== false) return true; } return false; } echo containsOneOfThoseWords($domain, $lookfor); ?> $ php search.php; echo 1 |