Summary of php array lookup function

  • 2021-08-03 09:39:52
  • OfStack

This article summarizes the php array lookup function with examples. Share it for your reference. The details are as follows:

Here, we provide three methods to judge whether a string includes the words we defined, which is more suitable for keyword filtering at addresses such as messages and comments. The example code is as follows:

$crud = array(' China ||| China ||| Earth ', 'kelon|||lerke|||sb', 'sesscxx'); 
$crud = join('|',$crud);
$crud = str_replace('|||', '|', $crud);
$pat  = "/({$crud})/i";
$txt = ' I know China you are sdfex Who! ! ';
preg_match/*_all*/($pat, $txt, $matches);
var_dump($matches);

Method 2, the code is as follows:
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(' China ||| China ||| Earth ', 'kelon|||lerke|||ssxb', 'aaa');
$test1 = ' I'm Chinese . There are many people in our country . The earth is full of people .-__-!! Good xx Sentence-making of .';
 
var_dump(checkcrud($test1, $crud));

Method 3, the code is as follows:
function lktest($v,$keyword){ 
foreach ($v as $k){
if (strpos($k,"|||")!==false){
    $kelon=explode("|||",$k);
    // Find the maximum value of array
    $b=count($kelon);
    foreach($kelon as $t){
        //echo $t.'<br>';
        if (preg_match('/'.$t.'/i',$keyword)){ 
            //echo " Sensitive keyword ";
            $a=$a+1;           
        }
    else{    
    $a='';
    }
            
    }
    // If the maximum value of the array is similar to that of the $a Is equal, all ||| The keywords all appeared
    if ($a==$b){
        echo " Sensitive keyword ";    
        }
   }
elseif(preg_match('/'.$k.'/i',$keyword)){
echo " Sensitive keyword ";
    } 
}
}

I hope this article is helpful to everyone's PHP programming.


Related articles: