perf: 关键词高亮
This commit is contained in:
@@ -226,18 +226,18 @@ if (!function_exists('get_platform')) {
|
||||
if (!function_exists('highlight_keywords')) {
|
||||
/**
|
||||
* 高亮关键词
|
||||
* @param string $item
|
||||
* @param string $keywords
|
||||
* @param array $class
|
||||
* @param string $text // 要处理的文本
|
||||
* @param string $keyword // 关键词
|
||||
* @param string|callable $repalce // 替换函数或字符串
|
||||
* @return string
|
||||
*/
|
||||
function highlight_keywords(string $text, string $keywords, array $class=[]): string
|
||||
function highlight_keywords(string $text, string $keyword, string|callable $replace): 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>';
|
||||
return preg_replace_callback('/' . preg_quote($keyword, '/') . '+/i', function($match) use($text, $replace) {
|
||||
if (empty($match)) return $text;
|
||||
if (is_string($replace)) return '<strong>' . $match[0] . '</strong>';
|
||||
if (is_callable($replace)) return $replace($match[0]);
|
||||
return $match[0];
|
||||
}, $text);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user