For Programmers: Free Programming Magazines  


Home > Archive > PHP Smarty Templates > April 2005 > Re: [SMARTY] google highlight









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [SMARTY] google highlight
Messju Mohr

2005-04-18, 3:59 pm

On Mon, Apr 18, 2005 at 12:59:55PM +0100, pete M wrote:
> I've got this "google highlight" modifier which was taken from the wiki
> http://smarty.incutio.com/?page=mod...oogle_highlight ta toma@etree.org
>
> I've modified it slightly as per the link below
> http://www.pastebin.com/272747
>
> The only problem is that "ocassionally" some of the phrases return
> spurious results..
>
> eg -
> search for "win do" is ok
> <span class="google_highlight">Win</span>chester House.<span
> class="google_highlight">do</span>c
>
> however "pa ss" returns extra tags as
> CP<span cla<span
> class="google_highlight">ss</span>="google_highlight">Pa</span><span
> class="google_highlight">ss</span>e<span
> class="google_highlight">ss</span>ments


str_replace() does replace already replaced parts. the
2-parameter-version of strtr() doesn't.

greetings
messju

> can a guru shed some light on thisn
>
> tia
>
> Pete
>
> --
> Smarty General Mailing List (http://smarty.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php

pete M

2005-04-18, 3:59 pm

ok I've tried teh strtr() and it seems to work.. here's the modified code

foreach (array_unique($_terms) as $val) {

$trans[$val] = "<span class='google_highlight'>$val</span>";
}
return strtr($text, $trans);

however tis case sensitive ...

Is there a way around that ??
tia
pete

Messju Mohr wrote:[color=darkred]
> On Mon, Apr 18, 2005 at 12:59:55PM +0100, pete M wrote:
>
>
>
> str_replace() does replace already replaced parts. the
> 2-parameter-version of strtr() doesn't.
>
> greetings
> messju
>
>
pete M

2005-04-18, 3:59 pm

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);
}
?>
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com