PHP教程

ThinkPHP空格模糊查找

本文主要是介绍ThinkPHP空格模糊查找,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
/**
     * @param $keywords string
     * @param $field string
     * @return array|string
     */
    public function advance_where_or($field,$keywords)
    {
        $keywords = trim($keywords);
        $keywords = preg_replace('/\s+/', ' ', $keywords);
        $where = [];
        if (strlen($keywords) > 0) {
            $keywordsArr = explode(" ", $keywords);
            if (count($keywordsArr) > 0) {
                $sql = "";
                $arrLen = count($keywordsArr);
                foreach ($keywordsArr as $k => $v) {
                    if ($k < $arrLen - 1) {
                        $sql .= " `" . $field . "` LIKE '%" . $v . "%' OR ";
                    }else{
                        $sql .= " `" . $field . "` LIKE '%" . $v . "%'";
                    }
                }
                $where = $sql;
            }
        }
        return $where;
    }

User::where(advance_where_or("nickname","张三 李四"))->select();

 

这篇关于ThinkPHP空格模糊查找的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!