如何用PHP搜索并高亮长尾中的关键词?
- 内容介绍
- 文章标签
- 相关推荐
本文共计92个文字,预计阅读时间需要1分钟。
phpfunction highlighter_text($text, $words) { $split_words=explode(' ', $words); foreach ($split_words as $word) { $color=red; // 假设高亮颜色为红色 $text=str_ireplace($word, $word, $text); } return $text;}
function highlighter_text($text, $words)
{
$split_words = explode( " " , $words );
foreach($split_words as $word)
{
$color = "#4285F4";
$text = preg_replace("|($word)|Ui" ,
"<span style=\"color:".$color.";\"><b>$1</b></span>" , $text );
}
return $text;
}
用法:
<?php $string = "I like chocolates and I like apples"; $words = "apple"; echo highlighter_text($string ,$words); ?>
本文共计92个文字,预计阅读时间需要1分钟。
phpfunction highlighter_text($text, $words) { $split_words=explode(' ', $words); foreach ($split_words as $word) { $color=red; // 假设高亮颜色为红色 $text=str_ireplace($word, $word, $text); } return $text;}
function highlighter_text($text, $words)
{
$split_words = explode( " " , $words );
foreach($split_words as $word)
{
$color = "#4285F4";
$text = preg_replace("|($word)|Ui" ,
"<span style=\"color:".$color.";\"><b>$1</b></span>" , $text );
}
return $text;
}
用法:
<?php $string = "I like chocolates and I like apples"; $words = "apple"; echo highlighter_text($string ,$words); ?>

