Tue 18th Apr 2006
Highlight Search Terms
Posted late at night, filed under Blogsome , Smarty Templates.I’m trying to get Search terms from within the Blog’s search engine to be highlighted. I’ve got the following code:
{if $smarty.request.s == ""} {$content} {else} {assign var=hilite value="<span class='hilite'>"|cat:$smarty.request.s|cat:"</span>"} {$content|replace:$smarty.request.s:$hilite} {/if}
This works fine, as long as the search term isn’t in a URL, or something like that. In that case, it breaks the URL.
To get around this, I need to only replace if it’s not inside an HTML tag. regex_replace to the rescue. This took me about 30 minutes to eventually figure out, a tipoff to wfinn at yakasha dot net, and the comment he made on the PHP documentation pages:
{if $smarty.request.s == ""} {$content} {else} {assign var=hilite value="<span class='hilite'>"|cat:$smarty.request.s|cat:"</span>"} {assign var=regex value="/(?![^< ]*?>)"|cat:$smarty.request.s|cat:"/"} {$content|regex_replace:$regex:$hilite} {/if}
Obviously, you’ll need to have some sort of CSS styling for the search term.
.hilite {color:red; border:1px dashed; padding:0 3px; 0 3px;}
This works really well for the first search.
For consecutive searches, it seems to get all mixed up as to what the search term is, and either highlights the previous search term, or both terms.
This behaviour is inconsistent, and annoying.
45 minutes after the fact.
And I may have been wrong with regard to case sensitivity. Apparently, Search matches Search and search, but search doesn’t match Search, only search.
Still, I’ll leave it implemented for now. Might be useful.
I could also check the referrer, and if it’s a search engine, highlight their search terms too.
1 hour after the fact.
Okay, now it’s weird. It is already highlighting search terms from Google, even though…
Aha! Moment of brilliance! I’d activated the Search Hilite plugin, and this was colouring my results. I must have chosen the same method of indicating the hilight as them!
I’ll be honest. That one is better than mine!
1 hour, 5 minutes after the fact.
Actually, I’m not so sure. It continues to highlight terms you’ve searched for last time, so it gets confusing.
I’ll rewrite it so it’s a bit nicer.
1 hour, 10 minutes after the fact.
Too hard to do right now. There doesn’t seem to be a way to get back references, which are required to replace case-insensitive text with the altered original, rather than the query as it was entered.
That is, Search get replaced by
<span class="hilite">search</span>not<span class="hilite">Search</span>.1 hour, 37 minutes after the fact.
And now I get why it seemed to be breaking sometimes before: the plugin only works with referrers, not with the current URL for within-site searches.
Luckily, I’ve got the code for the plugin, so I’ll try to fix it and give it back to the Blogsome team.
It should only be swapping a check around, so it checks first to see if there’s an
&sin the URL, and then the referrer if there isn’t.1 hour, 43 minutes after the fact.
bondage
1 hour, 47 minutes after the fact.
Hi Matt
One question off-topic.
Is it posible personalize the “error message”?:
Its mean: change “No posts made”.
Thank you.
3 weeks after the fact.
You might be able to do this using the
{$content|replace:"No posts made":"There are no posts that meet your search criteria"}trick.3 weeks after the fact.
Thank you very much.
3 weeks, 2 days after the fact.