找回密码
 快速注册
搜索
查看: 56|回复: 11

[已解决] 搜索带两个空格

[复制链接]

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

hbghlyj 发表于 2024-3-19 03:05 |阅读模式
本帖最后由 hbghlyj 于 2025-3-2 15:59 编辑 搜索
  1. wiki  aops
复制代码
时出錯(1064) You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'and t.subject LIKE '%aops%') ORDER BY tid DESC LIMIT 500' at line 1SELECT  t.tid, t.closed, t.author, t.authorid FROM forum_thread t WHERE  t.fid IN ('5','6','14','7','11','15','12','13','9','10','2') AND t.displayorder>='0' AND (t.subject LIKE '%wiki%' and  and t.subject LIKE '%aops%') ORDER BY tid DESC LIMIT 500

PHP Debug
No.FileLineCode
1search.php61require(%s)
2source/module/search/search_forum.php388discuz_database::query(%s)
3source/class/discuz/discuz_database.php142db_driver_mysqli->query(%s, false, false)
4source/class/db/db_driver_mysqli.php147db_driver_mysqli->halt(%s, %d, %s)
5source/class/db/db_driver_mysqli.php222break()

730

主题

1万

回帖

9万

积分

积分
93613
QQ

显示全部楼层

kuing 发表于 2024-3-19 03:27
就是变成连续两个 and 所以出问题?

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-3-19 03:28
kuing 发表于 2024-3-18 19:27
就是变成连续两个 and 所以出问题?

这个要怎么办才行呢😥

点评

你还记不记得改了哪里  发表于 2024-3-19 15:18
没權限看不到文件只能盲猜啊😥  发表于 2024-3-19 17:26
哪个文件总记得吧,在gitee上面看看呗  发表于 2024-3-19 19:09

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-3-19 21:52

建议像Approach Zero一样,关键词允许含空格。
关键词显示为一个文本块
Screenshot 2024-03-19 135121.png

730

主题

1万

回帖

9万

积分

积分
93613
QQ

显示全部楼层

kuing 发表于 2024-3-19 23:38
本帖最后由 kuing 于 2024-3-19 23:51 编辑 \source\function\function_search.php 中关于 searchkey 函数:
原定义:
  1. function searchkey($keyword, $field, $returnsrchtxt = 0) {
  2.         $srchtxt = '';
  3.         if($field && $keyword) {
  4.                 if(preg_match("(AND|\+|&|\s)", $keyword) && !preg_match("(OR|\|)", $keyword)) {
  5.                         $andor = ' AND ';
  6.                         $keywordsrch = '1';
  7.                         $keyword = preg_replace("/( AND |&| )/is", "+", $keyword);
  8.                 } else {
  9.                         $andor = ' OR ';
  10.                         $keywordsrch = '0';
  11.                         $keyword = preg_replace("/( OR |\|)/is", "+", $keyword);
  12.                 }
  13.                 $keyword = str_replace('*', '%', addcslashes($keyword, '%_'));
  14.                 $srchtxt = $returnsrchtxt ? $keyword : '';
  15.                 foreach(explode('+', $keyword) as $text) {
  16.                         $text = trim(daddslashes($text));
  17.                         if($text) {
  18.                                 $keywordsrch .= $andor;
  19.                                 $keywordsrch .= str_replace('{text}', $text, $field);
  20.                         }
  21.                 }
  22.                 $keyword = " AND ($keywordsrch)";
  23.         }
  24.         return $returnsrchtxt ? array($srchtxt, $keyword) : $keyword;
  25. }
复制代码

你当时修改成:
  1. function searchkey($keyword, $field, $returnsrchtxt = 0, $crit = 'and') {
  2.         $srchtxt = $keyword;
  3.         if($field && $keyword) {
  4.                 $keyword=addslashes($keyword);
  5.                 if ($crit=='regexp') {
  6.                         $text = trim($keyword);
  7.                         if($text)$keywordsrch .= str_replace('%{text}%', $text, str_replace('LIKE','REGEXP',$field));
  8.                 }else{
  9.                         $keyword=addslashes($keyword);
  10.                 }
  11.                 if($crit=='and'||$crit=='or'){
  12.                         foreach(explode(' ', $keyword) as $value) {
  13.                                 if(isset($text))$keywordsrch .= " $crit ";
  14.                                 $text = trim($value);
  15.                                 if($text)$keywordsrch .= str_replace('{text}', $text, $field);
  16.                         }
  17.                 }
  18.                 if ($crit=='exact') {
  19.                         $text = trim($keyword);
  20.                         if($text)$keywordsrch .= str_replace('{text}', $text, $field);
  21.                 }
  22.                 $keyword = " AND ($keywordsrch)";
  23.         }
  24.         $srchtxt || $srchtxt = '';
  25.         return $returnsrchtxt ? array($srchtxt, $keyword) : $keyword;
  26. }
复制代码

大概就是这个函数没改好。

foreach(explode(' ', $keyword) as $value) 用空格来拆分,当连续两空格时就存在空的 $value 吧?那么后面的 $text = trim($value); 也是空,随后的if($text)$keywordsrch .= str_replace('{text}', $text, $field); 便没有执行,所以 $keywordsrch 也就没定义了?
而前面如果不是 $crit=='regexp' 的情况,那 $text 也没定义,或许在别的地方有,我不了解,如果没定义的话,if(isset($text))$keywordsrch .= " $crit "; 也不执行?
总之这个函数改得感觉还有很多地方需要改。

3149

主题

8386

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65391
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2024-3-19 23:53
hbghlyj 发表于 2024-3-19 13:52
建议像Approach Zero一样,关键词允许含空格。关键词显示为一个文本块。


带两个关键词,Approach Zero搜索的URL為:
  1. https://approach0.xyz/search/?q=OR%20content%3ABanach%20space%2C%20OR%20content%3AHilbert%20space&p=1
复制代码


  1. OR content:Banach space, OR content:Hilbert space
复制代码

Screenshot 2024-03-19 135121.png
建議也採取這種編碼URL,关键词允许含空格

730

主题

1万

回帖

9万

积分

积分
93613
QQ

显示全部楼层

kuing 发表于 2024-3-20 00:15
还有你之前还设了个 debug=1 的方法,咋用的,我忘了

730

主题

1万

回帖

9万

积分

积分
93613
QQ

显示全部楼层

kuing 发表于 2024-3-20 00:30
还有 gitee.com/Discuz/DiscuzX/blob/v3.5/upload/source/module/search/search_forum.php#L363 中的
  1. $srcharr = $srchtype == 'fulltext' ? searchkey($keyword, "(p.message LIKE '%{text}%' OR p.subject LIKE '%{text}%')", true) : searchkey($keyword,"t.subject LIKE '%{text}%'", true);
复制代码

你改成了
  1. $binary=$_GET['Aa']=='1'?'BINARY ':'';$_GET['criteria'] || $_GET['criteria'] = 'and';$srcharr = $srchtype == 'fulltext' ? searchkey($keyword, "({$binary}p.message LIKE '%{text}%' OR {$binary}p.subject LIKE '%{text}%')", true, $_GET['criteria']) : searchkey($keyword,"{$binary}t.subject LIKE '%{text}%'", true, $_GET['criteria']);
复制代码

这块应该没问题,只是顺带提提。

730

主题

1万

回帖

9万

积分

积分
93613
QQ

显示全部楼层

kuing 发表于 2024-3-20 01:09
暂且在 searchkey 函数的第一步加入 $keyword = preg_replace('/\s+/', ' ', $keyword); 以解决连续空格问题。

至于是否需要继续完善该函数的编写,日后再说吧😌(关键我根本没基础,以上全靠半猜半蒙……

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 15:33

Powered by Discuz!

× 快速回复 返回顶部 返回列表