检查字符串包含数组字 [英] Check if string contains word in array

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

问题描述

这是一个聊天页面。我有一个 $字符串=这家伙是一个mothertrucker。我有BADWORDS数组: $劣品=阵列('车','出手',等)。我怎么能检查是否 $字符串包含 $坏?结果的任何话
到目前为止,我有:

 的foreach($劣品为$坏){
        如果(strpos($字符串,$不好)!== FALSE){
            //说不!
        }
        其他{
            // YES! }
        }

除了我这样做的时候,当一个字一个用户类型的 $劣品列表,输出是NO!其次是YES!所以出于某种原因,code上两次通过。


解决方案

 函数包含($ str中,数组$ ARR)
{
    的foreach($改编为$ a){
        如果(stripos函数($ A,$ STR)==假的!)返回true;
    }
    返回false;
}

This is for a chat page. I have a $string = "This dude is a mothertrucker". I have an array of badwords: $bads = array('truck', 'shot', etc). How could I check to see if $string contains any of the words in $bad?
So far I have:

        foreach ($bads as $bad) {
        if (strpos($string,$bad) !== false) {
            //say NO!
        }
        else {
            // YES!            }
        }

Except when I do this, when a user types in a word in the $bads list, the output is NO! followed by YES! so for some reason the code is running it twice through.

解决方案

function contains($str, array $arr)
{
    foreach($arr as $a) {
        if (stripos($a,$str) !== false) return true;
    }
    return false;
}

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

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