我正在使用 Bootstrap 2.3.1;我有一个搜索功能和结果页面。结果非常具体。
示例:
产品名称:AD-45。
输入搜索查询 = 产生大量结果。
AD45 没有结果...
如果第一个结果没有产生任何结果,我怎么能忽略“-”,如果第一个结果没有产生任何结果,我怎么能忽略“/”。
这是我的代码:
<?php
if(!isset($_GET['s'])) {
die('You must define a search term!');
}
$highlight = true; //highlight results or not
$search_in = array('html', 'htm', 'php'); //allowable filetypes to search in
$search_dir = '..'; //starting directory
$recursive = true; //should it search recursively or not
define('SIDE_CHARS', 80);
$file_count = 0;
$search_term = mb_strtolower($_GET['s'], 'UTF-8');
//$search_term = $_GET['s'];
$search_term_length = strlen($search_term);
$final_result = array();
$files = list_files($search_dir);
foreach($files as $file) {
$contents = file_get_contents($file);
preg_match("/\<title\>(.*)\<\/title\>/", $contents, $page_title); //getting page title
if(preg_match("#\<body.*\>(.*)\<\/body\>#si", $contents, $body_content)) { //getting content only between <body></body> tags
$clean_content = strip_tags($body_content[0]); //remove html tags
$clean_content = preg_replace('/\s+/', ' ', $clean_content); //remove duplicate whitespaces, carriage returns, tabs, etc
//$found = strpos_recursive($clean_content, $search_term);
$found = strpos_recursive(mb_strtolower($clean_content, 'UTF-8'), $search_term);
$final_result[$file_count]['page_title'][] = $page_title[1];
$final_result[$file_count]['file_name'][] = $file;
}
if($found && !empty($found)) {
for($z = 0; $z < count($found[0]); $z++) {
$pos = $found[0][$z][1];
$side_chars = SIDE_CHARS;
if($pos < SIDE_CHARS) {
$side_chars = $pos;
$pos_end = SIDE_CHARS + $search_term_length;
} else {
$pos_end = SIDE_CHARS * 2 + $search_term_length;
}
$pos_start = $pos - $side_chars;
$str = substr($clean_content, $pos_start, $pos_end);
$result = preg_replace('#' . $search_term . '#ui', '<span class="search">\0</span>', $str);
//$result = preg_replace('#'.$search_term.'#ui', '<span class="search">'.$search_term.'</span>', $str);
$final_result[$file_count]['search_result'][] = $result;
}
} else {
$final_result[$file_count]['search_result'][] = '';
}
$file_count++;
}
?>
我已将其添加到顶部附近作为解决方法,因为我的问题确实与部件号有关,但我不确定是否需要它:
function str_insert($str, $search, $insert) {
$index = strpos($str, $search);
if($index === false) {
return $str;
}
return substr_replace($str, $insert . $search, $index, strlen($search));
}
if(strpos($search_term, "-40") > -1 || strpos($search_term, "-45") > -1) {}
else { $search_term = str_insert($search_term, "4", "-"); }