【PHP】拆分关键词 模糊搜索 标题描红


     //(1)把关键词拆分成一个个汉字
        $str = $keywords;
        $length = mb_strlen($str);
        $tem = [];        
        for ($i = 0; $i < $length; $i++) {
           $tem[] = mb_substr($str, $i, 1);
        }
        //print_r($tem);
        //Array ( [0] => 北 [1] => 京 [2] => 学 [3] => 习 [4] => 好 [5] => 好 [6] => 的 )


     //(2)给关键字加 % 符号,并写成where条件,模糊查询数据
        foreach ($tem as $vo){
            $likes[] = '%'.$vo.'%';
        }
     //print_r($likes);
     //Array ( [0] => %北% [1] => %京% [2] => %学% [3] => %习% [4] => %好% [5] => %好% [6] => %的% )

       $where = [
            ['title','like',$likes,'or']
       ];
       $plist = Db::name('article')->where($where)->field('id,title')->select()->Toarray();
     

  

你可能感兴趣的:(PHP,TP,模糊搜索)