Posted By

apphp-snippets on 02/17/13


Tagged

php highlight


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

fehlercode42
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.')\[email protected]',
  9. '<strong style="background-color:yellow">$1</strong>',
  10. $str
  11. );
  12. }
  13. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: on November 25, 2018

the 8 best 20 watt portable bluetooth speaker topportablespeaker

You need to login to post a comment.