• 关键词多空格处理


    public function keyWord($condition) {
    
            //如果搜索的是上次的计算的结果,就直接返回
            $momon = Model()->table('goods')->limit(false)->field('goods_name')->select();
            if(is_array($condition) && !empty($condition['goods_name|goods_jingle']) && !empty($momon)) {
                $key = $condition['goods_name|goods_jingle'][1];
                $key = substr($key,1);
                $key = substr($key,0,-1);//用户输入关键字
                $key = $this->removePlace($key);
                $str = $this->tolower($key);
    
                //将用户关键词去空格和小写后,与数据库的商名比对,找出一条相似的字符串
                foreach($momon as $k=>$v) {
                    $data_lower = $this->tolower($v['goods_name']);//将要比对的数据库商品名小写,去对比,小写的用户关键字
                    $v = $this->removePlace($data_lower);//将数据库的商品名清除空格
                    $result = strpos($v, $str);
                    if($result !== false) {
                        $father_str_lower = $data_lower;
                        $father_str       = $v;
                        $chil_str         = $str;
                        break;
                    }
                }
                if(!empty($chil_str)) {
                    $check_arr = explode(' ',$father_str_lower);
                    foreach($check_arr as $v) {
                        $chil_str = preg_replace("/".$v."/", " ".$v." ", $chil_str);
                        $chil_str = preg_replace('/s{2,}/',' ',$chil_str);
                        $chil_str = trim($chil_str);
                    }
                    $_SESSION['chil_str'] = $chil_str;
                    $condition['goods_name|goods_jingle'][1] = "%".$chil_str."%";
                }
            }
            return $condition;
        }
        /*
         *中英字符串转小写
         * retun str
         * */
         public function tolower($a) {
            $b = str_split($a, 1);  
               $r = '';  
               foreach($b as $v){  
                   $v = ord($v);  
                   if($v >= 65 && $v<= 90){  
                       $v += 32;  
                   }  
                   $r .= chr($v);  
               }  
               return $r;  
         }
        /*
         * 去除空格
         * return str
         */
        public function removePlace($str) {
            $str = preg_replace("/s/is", "",$str);
            return $str;
        }
        /*
         *数据库所有商品名称 
         * return arr
         */
        public function allLowner_goodsName() {
            $data = Model()->table('goods')->limit(false)->select();
    
            return $data;
        }
  • 相关阅读:
    puppet master/agent
    puppet单机模型
    Nginx MogileFS 配置
    mogilefs 安装与配置
    CMakeLists.txt
    下载安装MariaDB Galera 10.1
    BZOJ1295: [SCOI2009]最长距离
    BZOJ2375: 疯狂的涂色
    BZOJ1260: [CQOI2007]涂色paint
    BZOJ2789: [Poi2012]Letters
  • 原文地址:https://www.cnblogs.com/bubaya/p/7565038.html
Copyright © 2020-2023  润新知