首页 ν 建站知识 ν 帝国CMS搜索支持多关键字词空格搜索结果 浏览量 收藏文章 海报

帝国CMS搜索支持多关键字词空格搜索结果

帝国CMS搜索优化(支持多关键字词空格搜索结果)

本方法带来的其他影响因素暂未测试(目前已知热门搜索关键字词调用的时候,关键字也是带有空格的。暂未发现其他)
简单优化了下官方的默认搜索系统,让搜索支持多个关键字词之间可以使用空格区分(两个关键词之间多少空格都无所谓,多个关键词也无所谓,都支持)
效果截图,搜索前:
 

效果截图,搜索后:
 后台搜索记录截图:
 

修改方法,打开/e/search/index.php
1、找到:
  1. //处理关键字
  2. function SearchDoKeyboardVar($keyboard){
  3.         $keyboard=RepPostVar2(trim($keyboard));
  4.         $keyboard=str_replace('  ','',$keyboard);
  5.         return $keyboard;
  6. }

修改为:
  1. //处理关键字
  2. function SearchDoKeyboardVar($keyboard){
  3.         $keyboard=RepPostVar2(trim($keyboard));
  4.         //$keyboard=str_replace('  ','',$keyboard);
  5. /*xuan 搜索关键字优化*/
  6. $keyboard=preg_replace('/[\s]+/s', ' ', $keyboard);
  7. /*xuan 搜索关键字优化*/
  8.         return $keyboard;
  9. }

2、找到:
  1. //(有两处,修改第二处)
  2. $where=$f." LIKE '%".$keyboard."%'";
  3. 7.0版为:
  4. $where=$f." LIKE '%".str_replace(" ","%",$keyboard)."%'";
  5.  

修改为:
  1. /*xuan 搜索关键字优化*/
  2. preg_match_all("/[^\s]+/s",$keyboard,$keyarr);
  3. foreach ($keyarr[0] as $val){
  4. $tj.=$f." like '%".$val."%' or ";
  5. }
  6. $where=substr($tj,0,-4);
  7. /*xuan 搜索关键字优化*/


这样即可。
懒人直接复制以下代码替换/e/search/index.php中所有代码即可
/e/search/index.php:
  1. <?php
  2. require("../class/connect.php");
  3. require("../class/db_sql.php");
  4. require("../data/dbcache/class.php");
  5. require("../class/q_functions.php");
  6. eCheckCloseMods('search');//关闭模块
  7. $link=db_connect();
  8. $empire=new mysqlquery();
  9.  
  10. //处理关键字
  11. function SearchDoKeyboardVar($keyboard){
  12.         $keyboard=RepPostVar2(trim($keyboard));
  13.         //$keyboard=str_replace('  ','',$keyboard);
  14. /*xuan 搜索关键字优化*/
  15. $keyboard=preg_replace('/[\s]+/s', ' ', $keyboard);
  16. /*xuan 搜索关键字优化*/
  17.         return $keyboard;
  18. }
  19.  
  20. //返回SQL
  21. function SearchDoKeyboard($f,$hh,$keyboard){
  22.         $where='';
  23.         $keyboard=SearchDoKeyboardVar($keyboard);
  24.         if(empty($keyboard))
  25.         {
  26.                 return "";
  27.         }
  28.         if(!empty($hh))
  29.         {
  30.                 if($hh=='LT')//小于
  31.                 {
  32.                         $where=$f."<'".$keyboard."'";
  33.                 }
  34.                 elseif($hh=='GT')//大于
  35.                 {
  36.                         $where=$f.">'".$keyboard."'";
  37.                 }
  38.                 elseif($hh=='EQ')//等于
  39.                 {
  40.                         $where=$f."='".$keyboard."'";
  41.                 }
  42.                 elseif($hh=='LE')//小于等于
  43.                 {
  44.                         $where=$f."<='".$keyboard."'";
  45.                 }
  46.                 elseif($hh=='GE')//大于等于
  47.                 {
  48.                         $where=$f.">='".$keyboard."'";
  49.                 }
  50.                 elseif($hh=='NE')//不等于
  51.                 {
  52.                         $where=$f."<>'".$keyboard."'";
  53.                 }
  54.                 elseif($hh=='IN')//包含
  55.                 {
  56.                         $kr=explode(' ',$keyboard);
  57.                         $kcount=count($kr);
  58.                         $kbs='';
  59.                         $dh='';
  60.                         for($i=0;$i<$kcount;$i++)
  61.                         {
  62.                                 $kr[$i]=(float)$kr[$i];
  63.                                 if(empty($kr[$i]))
  64.                                 {
  65.                                         continue;
  66.                                 }
  67.                                 if($kbs)
  68.                                 {
  69.                                         $dh=',';
  70.                                 }
  71.                                 $kbs.=$dh."'".$kr[$i]."'";
  72.                         }
  73.                         if($kbs)
  74.                         {
  75.                                 $where=$f." IN (".$kbs.")";
  76.                         }
  77.                         else
  78.                         {
  79.                                 return '';
  80.                         }
  81.                 }
  82.                 elseif($hh=='BT')//范围
  83.                 {
  84.                         $keyboard=ltrim($keyboard);
  85.                         if(!strstr($keyboard,' '))
  86.                         {
  87.                                 return '';
  88.                         }
  89.                         $kr=explode(' ',$keyboard);
  90.                         $kr[0]=(float)$kr[0];
  91.                         $kr[1]=(float)$kr[1];
  92.                         if(!trim($kr[0])||!trim($kr[1]))
  93.                         {
  94.                                 return '';
  95.                         }
  96.                         $where=$f." BETWEEN '".$kr[0]."' and '".$kr[1]."'";
  97.                 }
  98.                 else//相似
  99.                 {
  100.                         $where=$f." LIKE '%".$keyboard."%'";
  101.                 }
  102.         }
  103.         else
  104.         {
  105.                 //$where=$f." LIKE '%".str_replace(" ","%",$keyboard)."%'";
  106. /*xuan 搜索关键字优化*/
  107. preg_match_all("/[^\s]+/s",$keyboard,$keyarr);
  108. foreach ($keyarr[0] as $val){
  109. $tj.=$f." like '%".$val."%' or ";
  110. }
  111. $where=substr($tj,0,-4);
  112. /*xuan 搜索关键字优化*/
  113.         }
  114.         return $where;
  115. }
  116.  
  117. //变量
  118. if($_GET['searchget']==1)
  119. {
  120.         $_POST=$_GET;
  121. }
  122.  
  123. $ip=egetip();
  124. $searchtime=time();
  125. $getvar=$_POST['getvar'];
  126. if(empty($getvar))
  127. {
  128.         $getfrom="history.go(-1)";
  129.         $dogetvar='';
  130. }
  131. else
  132. {
  133.         $getfrom="../../search/";
  134.         $dogetvar="&getvar=1";
  135. }
  136. //返回
  137. $getfrom=DoingReturnUrl($getfrom,$_POST['ecmsfrom']);
  138. //搜索用户组
  139. if($public_r['searchgroupid'])
  140. {
  141.         $psearchgroupid=$public_r['searchgroupid'];
  142.         @include("../data/dbcache/MemberLevel.php");
  143.         $searchgroupid=(int)getcvar('mlgroupid');
  144.         if($level_r[$searchgroupid][level]<$level_r[$psearchgroupid][level])
  145.         {
  146.                 printerror("NotLevelToSearch",$getfrom,1);
  147.         }
  148. }
  149. //搜索间隔
  150. $lastsearchtime=getcvar('lastsearchtime');
  151. if($lastsearchtime)
  152. {
  153.         if($searchtime-$lastsearchtime<$public_r[searchtime])
  154.         {
  155.                 printerror("SearchOutTime",$getfrom,1);
  156.         }
  157. }
  158. //搜索字段
  159. $searchclass=$_POST['show'];
  160. if(empty($searchclass)||@strstr($searchclass," "))
  161. {
  162.         printerror("SearchNotRecord",$getfrom,1);
  163. }
  164. //时间范围
  165. $add='';
  166. $addtime='';
  167. $starttime=RepPostVar($_POST['starttime']);
  168. if(empty($starttime))
  169. {
  170.         $starttime="0000-00-00";
  171. }
  172. $endtime=RepPostVar($_POST['endtime']);
  173. if(empty($endtime))
  174. {
  175.         $endtime="0000-00-00";
  176. }
  177. if($endtime!="0000-00-00")
  178. {
  179.         $addtime=" and (newstime BETWEEN '".to_time($starttime." 00:00:00")."' and '".to_time($endtime." 23:59:59")."')";
  180. }
  181. //价格
  182. $addprice='';
  183. $startprice=(int)$_POST['startprice'];
  184. $endprice=(int)$_POST['endprice'];
  185. if($endprice)
  186. {
  187.         $addprice=" and (price BETWEEN ".$startprice." and ".$endprice.")";
  188. }
  189. //搜索栏目及表
  190. $classid=RepPostVar($_POST['classid']);
  191. $s_tbname=RepPostVar($_POST['tbname']);
  192. $s_tempid=(int)$_POST['tempid'];
  193. $trueclassid=0;
  194. if($classid)//按栏目
  195. {
  196.         if(strstr($classid,","))//多栏目
  197.         {
  198.                 $son_r=sys_ReturnMoreClass($classid,1);
  199.                 $trueclassid=$son_r[0];
  200.                 $add.=' and ('.$son_r[1].')';
  201.         }
  202.         else
  203.         {
  204.                 $trueclassid=intval($classid);
  205.                 $add.=$class_r[$trueclassid][islast]?" and classid='$trueclassid'":" and ".ReturnClass($class_r[$trueclassid][sonclass]);
  206.         }
  207.         $tbname=$class_r[$trueclassid][tbname];
  208.         $modid=$class_r[$trueclassid][modid];
  209. }
  210. elseif($s_tbname)//按数据表
  211. {
  212.         $tbnamenum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewstable where tbname='$s_tbname' limit 1");
  213.         if(!$tbnamenum)
  214.         {
  215.                 printerror("SearchNotRecord",$getfrom,1);
  216.         }
  217.         $tbname=$s_tbname;
  218.         //模型id
  219.         $thestemp_r=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where tempid='$s_tempid'");
  220.         if(empty($thestemp_r['modid']))
  221.         {
  222.                 printerror("SearchNotRecord",$getfrom,1);
  223.         }
  224.         $modid=$thestemp_r['modid'];
  225. }
  226. else
  227. {
  228.         $tbname=$public_r['tbname'];
  229.         $modid=0;
  230. }
  231. //表不存在
  232. if(empty($tbname)||InfoIsInTable($tbname))
  233. {
  234.         printerror("SearchNotRecord",$getfrom,1);
  235. }
  236. //标题分类
  237. $ttid=RepPostVar($_POST['ttid']);
  238. $truettid=0;
  239. if($ttid)
  240. {
  241.         if(strstr($ttid,","))//多标题分类
  242.         {
  243.                 $son_r=sys_ReturnMoreTT($ttid);
  244.                 $truettid=$son_r[0];
  245.                 $add.=' and ('.$son_r[1].')';
  246.         }
  247.         else
  248.         {
  249.                 $truettid=intval($ttid);
  250.                 $add.=" and ttid='$truettid'";
  251.         }
  252. }
  253. //会员
  254. $member=$_POST['member'];
  255. if($member==1)
  256. {
  257.         $add.=' and ismember=1';
  258. }
  259. elseif($member==2)
  260. {
  261.         $add.=' and ismember=0';
  262. }
  263. //模型
  264. $tempr=array();
  265. if(empty($class_r[$trueclassid][searchtempid]))
  266. {
  267.         if(empty($modid))
  268.         {
  269.                 $tempr=$empire->fetch1("select modid from ".GetTemptb("enewssearchtemp")." where isdefault=1 limit 1");
  270.         }
  271.         else
  272.         {
  273.                 $tempr[modid]=$modid;
  274.         }
  275. }
  276. else
  277. {
  278.         $tempr[modid]=$modid;
  279. }
  280.  
  281. //关键字
  282. $keyboard=$_POST['keyboard'];
  283. $keyboardone=0;
  284. if(is_array($keyboard))
  285. {}
  286. elseif(strstr($keyboard,','))
  287. {
  288.         $keyboard=explode(',',$keyboard);
  289. }
  290. else
  291. {
  292.         $keyboard=trim($keyboard);
  293.         $len=strlen($keyboard);
  294.         if($len<$public_r[min_keyboard]||$len>$public_r[max_keyboard])
  295.         {
  296.                 printerror("MinKeyboard",$getfrom,1);
  297.         }
  298.         $keyboardone=1;
  299. }
  300.  
  301. //符号
  302. $hh=$_POST['hh'];
  303. $hhone=0;
  304. if(is_array($hh))
  305. {}
  306. elseif(strstr($hh,','))
  307. {
  308.         $hh=explode(',',$hh);
  309. }
  310. else
  311. {
  312.         $hhone=1;
  313. }
  314.  
  315. //字段
  316. if(!is_array($searchclass))
  317. {
  318.         $searchclass=explode(',',$searchclass);
  319. }
  320.  
  321. $andor=$_POST['andor'];
  322. $andor=$andor=='and'?'and':'or';
  323.  
  324. $mr=$empire->fetch1("select searchvar,tbname from {$dbtbpre}enewsmod where mid='$tempr[modid]'");
  325. if(!strstr($mr[searchvar],",price,"))//是否包含价格
  326. {
  327.         $addprice="";
  328.         $startprice=0;
  329.         $endprice=0;
  330. }
  331. //搜索特殊字段
  332. $mr[searchvar].='id,keyboard,userid,username,';
  333. $where='';
  334. $newsearchclass='';
  335. $count=count($searchclass);
  336. for($i=0;$i<$count;$i++)
  337. {
  338.         if(empty($searchclass[$i]))
  339.         {
  340.                 continue;
  341.         }
  342.         $searchclass[$i]=str_replace(',','',$searchclass[$i]);
  343.         if(!strstr($mr[searchvar],",".$searchclass[$i].","))
  344.         {
  345.                 continue;
  346.         }
  347.         $searchclass[$i]=RepPostVar($searchclass[$i]);
  348.         $dh=empty($newsearchclass)?'':',';
  349.         $newsearchclass.=$dh.$searchclass[$i];
  350.         $dohh=$hhone==1?$hh:$hh[$i];
  351.         $dokeyboard=$keyboardone==1?$keyboard:$keyboard[$i];
  352.         $onewhere=SearchDoKeyboard($searchclass[$i],$dohh,$dokeyboard);
  353.         if($onewhere)
  354.         {
  355.                 $or=empty($where)?'':' '.$andor.' ';
  356.                 $where.=$or.'('.$onewhere.')';
  357.         }
  358. }
  359. //参数错
  360. if(empty($newsearchclass))
  361. {
  362.         printerror("SearchNotRecord",$getfrom,1);
  363. }
  364. if($where)
  365. {
  366.         $add.=' and ('.$where.')';
  367. }
  368. $allwhere=$add.$addtime.$addprice;
  369. $keyboard=$keyboardone==1?SearchDoKeyboardVar($keyboard):'';
  370. $andsql=addslashes($allwhere);
  371. if(strlen($newsearchclass)>250||strlen($classid)>200||strlen($andsql)>3000||strlen($keyboard)>100||strlen($ttid)>200)
  372. {
  373.         printerror("SearchNotRecord",$getfrom,1);
  374. }
  375. //验证码
  376. $checkpass=md5($allwhere.$tbname);
  377. $query="select count(*) as total from {$dbtbpre}ecms_".$tbname.($allwhere?' where '.substr($allwhere,5):'');
  378. $search_r=$empire->fetch1("select searchid from {$dbtbpre}enewssearch where checkpass='$checkpass' limit 1");
  379. $searchid=$search_r[searchid];
  380. //排序
  381. $orderby=RepPostVar($_POST['orderby']);
  382. $myorder=(int)$_POST['myorder'];
  383. if($orderby)
  384. {
  385.         $orderr=ReturnDoOrderF($tempr[modid],$orderby,$myorder);
  386.         $orderby=$orderr['returnf'];
  387. }
  388. else
  389. {
  390.         $orderby='newstime';
  391. }
  392. //是否有历史记录
  393. if($searchid)
  394. {
  395.     $search_num=$empire->gettotal($query);
  396.         $sql=$empire->query("update {$dbtbpre}enewssearch set searchtime='$searchtime',result_num='$search_num',onclick=onclick+1,orderby='$orderby',myorder='$myorder',tempid='$s_tempid' where searchid='$searchid'");
  397.         if(empty($search_num))
  398.         {
  399.                 $searchid=0;
  400.         }
  401. }
  402. else
  403. {
  404.         $search_num=$empire->gettotal($query);
  405.         if(empty($search_num))
  406.         {
  407.                 $searchid=0;
  408.         }
  409.         else
  410.         {
  411.                 $iskey=$keyboardone==1?0:1;
  412.                 $sql=$empire->query("insert into {$dbtbpre}enewssearch(searchtime,keyboard,searchclass,result_num,searchip,classid,onclick,orderby,myorder,checkpass,tbname,tempid,iskey,andsql,trueclassid) values('$searchtime','$keyboard','$newsearchclass','$search_num','$ip','$classid',1,'$orderby','$myorder','$checkpass','$tbname','$s_tempid','$iskey','$andsql','$trueclassid')");
  413.                 $searchid=$empire->lastid();
  414.         }
  415. }
  416. //设置最后搜索时间
  417. $set1=esetcookie("lastsearchtime",$searchtime,$searchtime+3600*24);
  418. if(!$searchid)
  419. {
  420.         printerror("SearchNotRecord",$getfrom,1);
  421. }
  422. else
  423. {
  424.         Header("Location:result/?searchid=$searchid".$dogetvar);
  425. }
  426. db_close();
  427. $empire=null;
  428. ?>


本方法兼容帝国CMS7.0/7.2,其他版本暂未测试。
 
打赏 赞( )
微信
支付宝
微信二维码图片

微信扫描二维码打赏

支付宝二维码图片

支付宝扫描二维码打赏

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表

技术栈-技术优质的资源信息

加入技术栈 联系我们