|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
bimal Smarty Elite
Joined: 19 Apr 2007 Posts: 423
|
Posted: Wed Jun 17, 2009 5:10 pm Post subject: A tip a day - Highlight a portion of text, searching a word |
|
|
In this tutorial, I am describing my trick how I built and highlighted a search term in my dreams project. URL: http://dreams.gaindakot.com.np/
Ok, basically, this is an e-book of dictionary of dreams from wordsworth publication. Now, you can search your dream terms in the search box there.
But the problem is that many search terms may match in various dreams descriptions. For example, if you search for 'brother', the text might match with the words like 'mountain', and 'kiss' which is VALID. (Their descriptions have the search term - brother.
So, to see, in which part of the text, the match was found, I needed to highlight the search string. In my case, I wrote a small modifier to do this. See a sample usage below:
Code: | <div class="text">{$dreams[d].t|highlight:$search_tag}</div> |
hilight is a modifer that uses HTML/CSS to decorate the text. I am replacing the matched string with some piece of css/html combination to highlight. Watch the modifier below (modifier.highlight.php in your plugins folder)
Code: | <?php
/**
* Highlights a text by searching a word in it.
*/
function smarty_modifier_highlight(&$text='', $word='')
{
$new_text = $text;
if($word)
{
$word = ucwords($word);
$new_text = str_ireplace($word, "<span class='hilight'>{$word}</span>", $text);
}
return($new_text);
}
?> |
If you like, you can easily use this modifier. Just note that in a piece of text ($dreams[d].t), you should send a search word ($search_tag) correctly.
Important notice:
In this example, pulling the searching results is done through sql full text search. But while highlighting the text, it is NOT so intelligent. Hence, some oddities are there. For example, if you search 'hat', it is likely to match a useless/common word 'that' because, 'that' contains 'hat' in its spelling. My project accepts this error.
Love you all the readers! Enjoy highlighting most important moments of your life. |
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Thu Jun 18, 2009 6:20 am Post subject: |
|
|
Useful like all your plugins - thx! |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Jun 18, 2009 1:53 pm Post subject: |
|
|
If you want to find whole words (so "hat" won't find "that"), use preg_replace with word boundaries. I simplified and debugged your code a bit too.
Code: | function smarty_modifier_highlight($text='', $word='')
{
if(strlen($text) > 0 && strlen($word) > 0)
{
return preg_replace('/\b('.preg_quote($word).')\b/', '<span class="highlight">${1}</span>', $text);
}
return($text);
} |
|
|
Back to top |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Thu Jun 18, 2009 2:26 pm Post subject: |
|
|
thx a lot ... |
|
Back to top |
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|