Posted By

apphp-snippets on 02/17/13


Tagged

php highlight


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Abiotic
fackz


Highlight specific words in a phrase with PHP


 / Published in: PHP
 

URL: http://www.apphp.com/index.php?snippet=php-highlight-specific-words-in-a-phrase

You may use this code to highlight specific words in your displaying search results.

  1. <?php
  2. function highlight($str, $words){
  3. if(!is_array($words) || empty($words) || !is_string($str)){
  4. return false;
  5. }
  6. $arr_words = implode('|', $words);
  7. return preg_replace(
  8. '@\b('.$arr_words.')\b@si',
  9. '<strong style="background-color:yellow">$1</strong>',
  10. $str
  11. );
  12. }
  13. ?>

Report this snippet  

You need to login to post a comment.