Return to Snippet

Revision: 62341
at February 17, 2013 20:27 by apphp-snippets

Initial Code
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.')\[email protected]',
        '<strong style="background-color:yellow">$1</strong>',

Initial URL

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

Initial Title
Highlight specific words in a phrase with PHP

Initial Tags

Initial Language