Return to Snippet

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


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

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

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
php

Initial Language
PHP