&引用;例如;PHP中的搜索和突出显示
我有一个品牌列表,想提供一个突出显示的搜索功能。例如,有以下品牌&引用;例如;PHP中的搜索和突出显示,php,search,highlighting,Php,Search,Highlighting,我有一个品牌列表,想提供一个突出显示的搜索功能。例如,有以下品牌 苹果 Cewe颜色 欧莱雅 微软 麦当劳 汤姆·泰勒 然后用户在搜索表单中键入lor。我正在使用以下代码段进行搜索 class search { private function simplify($str) { return str_replace(array('&',' ',',','.','?','|','\'','"'), '', iconv('UTF-8', 'ASCII//TRANSLIT',
- 苹果
- Cewe颜色
- 欧莱雅
- 微软
- 麦当劳
- 汤姆·泰勒
lor
。我正在使用以下代码段进行搜索
class search {
private function simplify($str) {
return str_replace(array('&',' ',',','.','?','|','\'','"'), '', iconv('UTF-8', 'ASCII//TRANSLIT', $str));
}
public function do_search($search) {
$search = self::simplify($search);
$found = array();
foreach (self::$_brands as $brand) {
if (mb_strstr(self::simplify($brand['name']), $search) !== false) $found[]= $brand;
}
return $found;
}
}
这给了我:
- Cewe颜色
- 欧莱雅
- 汤姆·泰勒
Cewe颜色
- 欧莱雅
tomtailor
str_replace()
来完成,但并非所有情况下都符合我的需要$highlighted=str_replace($search,$search,$brand);
$highlighted = str_replace($search, "<b>$search</b>", $brand);
这是最简单的方法。:)
还与联邦快递合作;)
$\u品牌=阵列
(
“苹果”,
“Cewe颜色”,
“欧莱雅”,
“微软”,
“麦当劳”,
“汤姆·泰勒”
);
$q='lor';
$search=clean($q);
foreach($\品牌为$key=>$brand){
$brand=清洁($brand);
$x=stripos($brand,$search);
如果($x!==false){
$regexp=NULL;
$l=斯特伦($q);
对于($i=0;$i<$l;$i++){
$regexp.=mb_strotupper($q[$i]);
}
$regexp=substr($regexp,0,strlen($regexp)-2);
$new=$_品牌[$key];
$new=preg#u replace(“#”(“$regexp”)#ui”,“$0”,“$new”);
echo$new.“
”;
}
}
函数clean($string){
$string=iconv('UTF-8','ASCII//TRANSLIT//IGNORE',$string);
$string=preg#u replace('.[^\w]#ui',''.$string);
返回$string;
}
self:$\u brands
包含来自数据库的结果(包含列name
,name\u lower
,name\u translatit
,name\u simplified
)
类搜索{
私有函数translatit($str){
返回iconv('UTF-8','ASCII//TRANSLIT//IGNORE',str_replace(数组('a','u','ö','ß'),数组('a','u','o','s'),mb_strtolower($str));
}
私有函数简化($str){
返回preg_替换('/([^a-z0-9])/ui','',self::translit($str));
}
公共功能搜索($simplified){
$found=array();
foreach(self:$品牌作为$brand){
如果(mb_strstr($brand['name_simplified',$simplified)!==false)$found[]=$brand;
}
返回$found;
}
私有函数actionDefault(){
$search=$\u POST['search\u fld'];
$simplified=self::simplify($search);
$result=self::do_search($simplified);
$brands=array();
foreach(结果为$brand){
$hl_start=mb_strpos($brand['name_simplified',$simplified);
$hl_len=mb_strlen($simplified);
$brand_len=mb_strlen($brand['name']);
$tmp='';
$cnt_额外=0;
$start_tag=false;
$end_tag=false;
对于($i=0;$i<$brand_len;$i++){
如果($i-$cnt_extra)
我可以想出一个解决方案,但它可能效率低下。我已经考虑过类似的问题。我将对此进行一些调整(我认为可以在foreach
循环中使用str\u replace
而不是preg\u replace
。但这会对我有所帮助。我会在解决方案完成后发布。@rabudde,我很快就能解决这个问题,但我对regex的缺乏知识没有帮助:)你可以编写正则表达式来替换“lor”和其间的每个字符。嗯,这应该可以,但是ucwords()
不适合像联邦快递或“ABC新闻”@rabudde这样的品牌,我对代码做了一些调整,现在可以了。ucwords()现在是多余的。@rabudde,*
是贪婪的,*?
是不冻结的,?
匹配任何字符0或1次,这是粘贴,它应该可以工作。。。已经尝试过:)查看我的更新答案,它应该可以使用更少的代码;)
$_brands = array
(
"Apple",
"Cewe Color",
"L'Oréal",
"Microsoft",
"McDonald's",
"Tom Tailor"
);
$q = 'lor';
$search = clean($q);
foreach($_brands as $key => $brand){
$brand = clean($brand);
$x = stripos($brand, $search);
if($x !== false){
$regexp = NULL;
$l = strlen($q);
for($i = 0; $i < $l; $i++){
$regexp .= mb_strtoupper($q[$i]).'.?';
}
$regexp = substr($regexp, 0, strlen($regexp) - 2);
$new = $_brands[$key];
$new = preg_replace('#('.$regexp.')#ui', '<b>$0</b>', $new);
echo $new."<br />";
}
}
function clean($string){
$string = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $string);
$string = preg_replace('#[^\w]#ui', '', $string);
return $string;
}
class search {
private function translit($str) {
return iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', str_replace(array('ä', 'ü', 'ö', 'ß'), array('a', 'u', 'o', 's'), mb_strtolower($str)));
}
private function simplify($str) {
return preg_replace('/([^a-z0-9])/ui', '', self::translit($str));
}
public function do_search($simplified) {
$found = array();
foreach (self::$_brands as $brand) {
if (mb_strstr($brand['name_simplified'], $simplified) !== false) $found[]= $brand;
}
return $found;
}
private function actionDefault() {
$search = $_POST['search_fld'];
$simplified = self::simplify($search);
$result = self::do_search($simplified);
$brands = array();
foreach ($result as $brand) {
$hl_start = mb_strpos($brand['name_simplified'], $simplified);
$hl_len = mb_strlen($simplified);
$brand_len = mb_strlen($brand['name']);
$tmp = '';
$cnt_extra = 0;
$start_tag = false;
$end_tag = false;
for ($i = 0; $i < $brand_len; $i++) {
if (($i - $cnt_extra) < mb_strlen($brand['name_simplified']) && mb_substr($brand['name_translit'], $i, 1) != mb_substr($brand['name_simplified'], $i - $cnt_extra, 1)) $cnt_extra++;
if (($i - $cnt_extra) == $hl_start && !$start_tag) {
$tmp .= '<b>';
$start_tag = true;
}
$tmp .= mb_substr($brand['name'], $i, 1);
if (($i - $cnt_extra + 1) == ($hl_start + $hl_len) && !$end_tag) {
$tmp .= '</b>';
$end_tag = true;
}
}
if ($start_tag && !$end_tag) $tmp .= '</b>';
$brands[] = "<a href=\"/brand/" . rawurlencode($brand['name']) . "\">" . $tmp . "</a>";
}
echo implode(' | ', $brands);
}
}