PHP搜索和高亮字符串中的关键字

WBOY
發布: 2016-07-25 08:42:46
原創
1093 人瀏覽過
  1. function highlighter_text($text, $words)
  2. {
  3. $split_words = explode( " " , $words );
  4. foreach($split_words as $word)
  5. {
  6. $color = "#4285F4";
  7. $text = preg_replace("|($word)|Ui" ,
  8. "$1" , $text );
  9. }
  10. return $text;
  11. }
复制代码

用法:
  1. $string = "I like chocolates and I like apples";
  2. $words = "apple";
  3. echo highlighter_text($string ,$words);
  4. ?>
复制代码

PHP


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!