[英]Highlight keyword on search results php
I am trying to make any keyword bold when the user types the current keyword. 我试图在用户键入当前关键字时使任何关键字变为粗体。
I have this code and it is working fine. 我有此代码,并且工作正常。
for($k=$no_words; $k>0 ;$k--) {
$w=trim($search_array[$k-1]);
if($w!='')
{
$result[$i]['title'] = preg_replace('/(' . preg_quote($search_array[$k-1], '/') . ')/siU', '<b>\\1</b>', $result[$i]['title']);
$result[$i]['description'] = preg_replace('/(' . preg_quote($search_array[$k-1], '/') . ')/siU', '<b>\\1</b>', $result[$i]['description']);
}
}
My problem is as follows: 我的问题如下:
I have this keyword: this is my keyword 我有这个关键字: 这是我的关键字
When I type: " this is my keyword " I get this result: " this is my keyword " 当我键入:“这是我的关键字”我得到以下结果:“ 这是我的关键字 ”
But when I type: " This is keyword " I get this result: "this is my keyword" without the words in the result being bolded. 但是,当我键入:“这是关键字”时,我得到的结果是:“这是关键字”,结果中的单词未加粗。
What I doing wrong? 我做错了什么?
I suppose you need following: 我想您需要遵循以下条件:
$search_array = array_unique(explode(' ', $search));
foreach ($search_array as $k => $v)
{
$w = trim($v);
if ($w)
{
$result[$i]['title'] = preg_replace('/(' . preg_quote($w, '/') . ')/siU', '<b>\\1</b>', $result[$i]['title']);
$result[$i]['description'] = preg_replace('/(' . preg_quote($w, '/') . ')/siU', '<b>\\1</b>', $result[$i]['description']);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.