Heim  >  Artikel  >  Backend-Entwicklung  >  php数组查找函数总结_PHP

php数组查找函数总结_PHP

WBOY
WBOYOriginal
2016-05-31 19:27:19798Durchsuche

本文实例总结了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程序设计有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn