|
kuing
发表于 2024-3-19 23:38
本帖最后由 kuing 于 2024-3-19 23:51 编辑 \source\function\function_search.php 中关于 searchkey 函数:
原定义:
- function searchkey($keyword, $field, $returnsrchtxt = 0) {
- $srchtxt = '';
- if($field && $keyword) {
- if(preg_match("(AND|\+|&|\s)", $keyword) && !preg_match("(OR|\|)", $keyword)) {
- $andor = ' AND ';
- $keywordsrch = '1';
- $keyword = preg_replace("/( AND |&| )/is", "+", $keyword);
- } else {
- $andor = ' OR ';
- $keywordsrch = '0';
- $keyword = preg_replace("/( OR |\|)/is", "+", $keyword);
- }
- $keyword = str_replace('*', '%', addcslashes($keyword, '%_'));
- $srchtxt = $returnsrchtxt ? $keyword : '';
- foreach(explode('+', $keyword) as $text) {
- $text = trim(daddslashes($text));
- if($text) {
- $keywordsrch .= $andor;
- $keywordsrch .= str_replace('{text}', $text, $field);
- }
- }
- $keyword = " AND ($keywordsrch)";
- }
- return $returnsrchtxt ? array($srchtxt, $keyword) : $keyword;
- }
复制代码
你当时修改成:
- function searchkey($keyword, $field, $returnsrchtxt = 0, $crit = 'and') {
- $srchtxt = $keyword;
- if($field && $keyword) {
- $keyword=addslashes($keyword);
- if ($crit=='regexp') {
- $text = trim($keyword);
- if($text)$keywordsrch .= str_replace('%{text}%', $text, str_replace('LIKE','REGEXP',$field));
- }else{
- $keyword=addslashes($keyword);
- }
- if($crit=='and'||$crit=='or'){
- foreach(explode(' ', $keyword) as $value) {
- if(isset($text))$keywordsrch .= " $crit ";
- $text = trim($value);
- if($text)$keywordsrch .= str_replace('{text}', $text, $field);
- }
- }
- if ($crit=='exact') {
- $text = trim($keyword);
- if($text)$keywordsrch .= str_replace('{text}', $text, $field);
- }
- $keyword = " AND ($keywordsrch)";
- }
- $srchtxt || $srchtxt = '';
- return $returnsrchtxt ? array($srchtxt, $keyword) : $keyword;
- }
复制代码
大概就是这个函数没改好。
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 "; 也不执行?
总之这个函数改得感觉还有很多地方需要改。 |
|