inp*_*put 7 html php arrays search highlight
我正在使用此代码突出显示搜索关键字:
function highlightWords($string, $word)
{
$string = str_replace($word, "<span class='highlight'>".$word."</span>", $string);
/*** return the highlighted string ***/
return $string;
}
....
$cQuote = highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
Run Code Online (Sandbox Code Playgroud)
但是,这只突出显示一个关键字.如果用户输入多个关键字,则会缩小搜索范围,但不会突出显示任何字词.我怎么能突出一个以上的单词?
use*_*291 25
正则表达式是要走的路!
function highlight($text, $words) {
preg_match_all('~\w+~', $words, $m);
if(!$m)
return $text;
$re = '~\\b(' . implode('|', $m[0]) . ')\\b~';
return preg_replace($re, '<b>$0</b>', $text);
}
$text = '
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat.
';
$words = 'ipsum labore';
print highlight($text, $words);
Run Code Online (Sandbox Code Playgroud)
要以不区分大小写的方式匹配,请将"i"添加到正则表达式中
$re = '~\\b(' . implode('|', $m[0]) . ')\\b~i';
Run Code Online (Sandbox Code Playgroud)
注意:对于像"ä"这样的非英语字母,结果可能因地区而异.
gol*_*sky 17
PHP> 5.3.0,尝试preg_filter()
/**
* Highlighting matching string
* @param string $text subject
* @param string $words search string
* @return string highlighted text
*/
public function highlight($text, $words) {
$highlighted = preg_filter('/' . preg_quote($words, '/') . '/i', '<b><span class="search-highlight">$0</span></b>', $text);
if (!empty($highlighted)) {
$text = $highlighted;
}
return $text;
}
Run Code Online (Sandbox Code Playgroud)
假设单词是作为空格分隔的字符串输入的,您可以使用explode
$words = explode(' ', $term);
Run Code Online (Sandbox Code Playgroud)
虽然如果要确保没有多个空格,可能需要先从字符串中删除它们
$term = preg_replace('/\s+/', ' ', trim($term));
$words = explode(' ', $term);
Run Code Online (Sandbox Code Playgroud)
然后你必须生成一个替换数组
$highlighted = array();
foreach ( $words as $word ){
$highlighted[] = "<span class='highlight'>".$word."</span>"
}
Run Code Online (Sandbox Code Playgroud)
然后
str_replace($words, $highlighted, $string);
Run Code Online (Sandbox Code Playgroud)
所以把它放在一起吧
function highlightWords($string, $term){
$term = preg_replace('/\s+/', ' ', trim($term));
$words = explode(' ', $term);
$highlighted = array();
foreach ( $words as $word ){
$highlighted[] = "<span class='highlight'>".$word."</span>"
}
return str_replace($words, $highlighted, $string);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
这是仅突出显示匹配文本的简单函数。
function highlighter_text($text, $words)
{
$split_words = explode( " " , $words );
foreach($split_words as $word)
{
$color = "#e5e5e5";
$text = preg_replace("|($word)|Ui" ,
"<span style=\"background:".$color.";\"><b>$1</b></span>" , $text );
}
return $text;
}
Run Code Online (Sandbox Code Playgroud)
通话功能
小智 5
我使用了之前编写的正则表达式并替换\w
为[A-Za-z0-9_äöüÄÖÜ]
. 如您所见,我添加了变音符号äöüÄÖÜ
。我还删除了 ,\b
因此它将匹配搜索词的任何外观。
搜索词:
Su shamp
文字:
阳光闪亮洗发水
结果:
苏ñ闪亮shamp OO
private function getSearchTermToBold($text, $words)
{
preg_match_all('~[A-Za-z0-9_äöüÄÖÜ]+~', $words, $m);
if (!$m)
return $text;
$re = '~(' . implode('|', $m[0]) . ')~i';
return preg_replace($re, '<b>$0</b>', $text);
}
Run Code Online (Sandbox Code Playgroud)