Highlight Specific Words in a Phrase with PHP
You may use this code to highlight specific words in your displaying search results.
[View All Snippets]
[View All Snippets]
Show Plain Text »
- <?php
- function highlight($str, $words){
- if(!is_array($words) || empty($words) || !is_string($str)){
- return false;
- }
- $arr_words = implode('|', $words);
- return preg_replace(
- '@\b('.$arr_words.')\b@si',
- '<strong style="background-color:yellow">$1</strong>',
- $str
- );
- }
- ?>