fix: 产品搜索关键词高亮
This commit is contained in:
@@ -222,3 +222,22 @@ if (!function_exists('get_platform')) {
|
||||
return $platform;
|
||||
}
|
||||
}
|
||||
|
||||
if (!function_exists('highlight_keywords')) {
|
||||
/**
|
||||
* 高亮关键词
|
||||
* @param string $item
|
||||
* @param string $keywords
|
||||
* @param array $class
|
||||
* @return string
|
||||
*/
|
||||
function highlight_keywords(string $text, string $keywords, array $class=[]): string
|
||||
{
|
||||
return preg_replace_callback('/' . preg_quote($keywords, '/') . '+/i', function($match) use($text, $class) {
|
||||
if (empty($match)) {
|
||||
return $text;
|
||||
}
|
||||
return '<strong class="' . implode(' ', $class) . '">' . $match[0] . '</strong>';
|
||||
}, $text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user