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();

 

posted @ 2021-06-22 16:55  _迷途  阅读(140)  评论(0编辑  收藏  举报