Home > php教程 > php手册 > php数组查找函数总结

php数组查找函数总结

WBOY
Release: 2016-06-06 20:17:13
Original
1690 people have browsed it

这篇文章主要介绍了php数组查找函数,总结了三个常用的数组查找函数,可用于关键词过滤,是非常实用的技巧,需要的朋友可以参考下

本文实例总结了php数组查找函数。分享给大家供大家参考。具体如下:

这里提供三种方法来判断一个字符串中是否包括我们定义好的词,这比较适合于在留言,评论等地址进行关键词过滤,实例代码如下:

复制代码 代码如下:

$crud = array('中国|||我国|||大地', 'kelon|||lerke|||sb', 'sesscxx');
$crud = join('|',$crud);
$crud = str_replace('|||', '|', $crud);
$pat  = "/({$crud})/i";
$txt = '我知道中国你是sdfex谁!!';
preg_match/*_all*/($pat, $txt, $matches);
var_dump($matches);


方法二,代码如下:

复制代码 代码如下:

function checkcrud($str, $crud)
{
    if(is_array($crud) && !emptyempty($crud))
    {
        foreach($crud as $value)
        {
            if(strpos($value, '|||') !== false)
            {
                $cruds = explode('|||', $value);
                $num = count($cruds);
                $check = 0;
                foreach($cruds as $val)
                {
                    if(strpos($str, $val) !== false)
                    {
                        $check++;
                    }
                }
                if($check == $num)
                {
                    return true;
                }
            }
            else
            {
                if(strpos($str, $value) !== false)
                {
                    return true;
                }
            }
        }
        return false;
    }
}
$crud = array('中国|||我国|||大地', 'kelon|||lerke|||ssxb', 'aaa');
$test1 = '我是中国人.我国人很多.大地上全是人.-__-!!好xx的造句.';
 
var_dump(checkcrud($test1, $crud));


方法三,代码如下:

复制代码 代码如下:

function lktest($v,$keyword){
foreach ($v as $k){
if (strpos($k,"|||")!==false){
    $kelon=explode("|||",$k);
    //求数组最大值
    $b=count($kelon);
    foreach($kelon as $t){
        //echo $t.'
';
        if (preg_match('/'.$t.'/i',$keyword)){ 
            //echo "敏感关键字";
            $a=$a+1;           
        }
    else{    
    $a='';
    }
            
    }
    //如果数组最大值跟$a相等时,,则所有|||关键字都出现了
    if ($a==$b){
        echo "敏感关键字";    
        }
   }
elseif(preg_match('/'.$k.'/i',$keyword)){
echo "敏感关键字";
    } 
}
}

希望本文所述对大家的PHP程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template