Re: [SMARTY] google highlight

From: Date: Mon, 18 Apr 2005 13:41:42 +0000
Subject: Re: [SMARTY] google highlight
References: 1 2 3  Groups: php.smarty.general 
Request: Send a blank email to php-smarty-general+get-12532@lists.php.net to get a copy of this message
Thanks to messju here is the modified modifier ;-))))) <?php function smarty_modifier_highlight ($text, $search)
    {
        // Init vars
        static $_search_phrases;
        // Load saved terms if they have already been parsed.
        if (!$_terms = (array)$_search_phrases[$search]) {
            // pull out quoted strings
            preg_match_all( '/"(.*?)"/', $search, $_quotes);
            // split on whitespace
            $_terms = array_merge((array)$_quotes[1], explode(' ', preg_replace( '/".*?"/', ' ', $search )));
            $_search_phrases[$search] = $_terms;
        }
        // Loop through each term and highlight
        foreach (array_unique($_terms) as $val) {
            // Strip any stray non-matched double quotes and check for empty string
            if (!$val = trim(str_replace('"', '', $val))) continue;
            if (preg_match_all('!' . preg_quote($val, '!') . '!i', $text, $matches, PREG_PATTERN_ORDER)) {
                foreach ($matches[0] as $match) {
                    $trans[$match] = "<span class='google_highlight'>$match</span>";
                }
            }
        }
        return strtr($text, $trans);
    }
?>

« previous php.smarty.general (#12532) next »