PHP 检查字符串是否包含多个单词 [英] Php check if string contains multiple words

查看:89
本文介绍了PHP 检查字符串是否包含多个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上四处寻找可以做到这一点的东西,但它只能用一个词.

I have looked around the internet for something that will do this but it will only work with one word.

我正在尝试构建一个脚本来检测我网站的错误用户名,如果用户名包含数组中的任何单词,则会检测到错误的用户名.

I am trying to build a script that will detect a bad username for my site, the bad username will be detected if the username contains any of the words in an array.

这是我编写的代码,但没有成功.

Here's the code I made, but failed to work.

$bad_words = array("yo","hi");
$sentence = "yo";

if (strpos($bad_words,$sentence)==false) {
echo "success";
}

如果有人能帮助我,我将不胜感激.

If anybody could help me, I would appreciate it.

推荐答案

使用

substr_count

对于数组使用以下函数

function substr_count_array( $haystack, $needle ) {
     $count = 0;
     foreach ($needle as $substring) {
          $count += substr_count( $haystack, $substring);
     }
     return $count;
}

这篇关于PHP 检查字符串是否包含多个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆