PHP如何实现搜索并高亮显示文本中的关键词?

更新于
2026-09-26 05:34:12
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计93个文字,预计阅读时间需要1分钟。

PHP如何实现搜索并高亮显示文本中的关键词?

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); ?>

PHP如何实现搜索并高亮显示文本中的关键词?

本文共计93个文字,预计阅读时间需要1分钟。

PHP如何实现搜索并高亮显示文本中的关键词?

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); ?>

PHP如何实现搜索并高亮显示文本中的关键词?