RSS Forums RSS
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1970 | Replies: 7
Reply
Join Date: May 2007
Posts: 5
Reputation: aniltc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
aniltc aniltc is offline Offline
Newbie Poster

Solution search key highlighting ?

  #1  
May 21st, 2007
hi

How can we highlite(selecting) a search key.


suppose i want to search php from database.so i am displaying a paragraph from the database.I want php to be highlited.How can i do this

please help me

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: New Jersey
Posts: 1,422
Reputation: stymiee is on a distinguished road 
Rep Power: 5
Solved Threads: 34
Moderator
stymiee's Avatar
stymiee stymiee is offline Offline
He's No Good To Me Dead

Re: search key highlighting ?

  #2  
May 21st, 2007
Just use str_replace() to swap out the word with the same word wrapped in a <span> tag that uses CSS to change the background color of the word. Not very hard to do at all.
John Conde
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
Reply With Quote  
Join Date: May 2007
Posts: 5
Reputation: aniltc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
aniltc aniltc is offline Offline
Newbie Poster

Question Re: search key highlighting ?

  #3  
May 21st, 2007
Thanks

This is what i have done so far

<?php
if(!isset($searchItem)) { $searchItem = "randomstringset"; }
function callback($buffer) {

global $searchItem;
// surround search item items with highlight class
return (ereg_replace($searchItem, "<span class='highlight'>$searchItem</span>", $buffer));

}

ob_start("callback");
?>

But i am not looking this simple one.
what i am looking here is
i searched a keyword (for eg php ).After searching I got some results from the database.if the result has a keyword php,then that all (php) word should be highlited .
please help me
Reply With Quote  
Join Date: May 2007
Location: Bucharest, RO
Posts: 67
Reputation: johny_d is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 4
johny_d's Avatar
johny_d johny_d is offline Offline
Junior Poster in Training

Re: search key highlighting ?

  #4  
May 22nd, 2007
  1. <?php
  2. $string = 'But i am not looking this simple one. what i am looking here is i searched a keyword (for eg php ). After searching I got some results from the database. if the result has a keyword php, then that all (php) word should be highlited. please help me';
  3. $search_str = 'php';
  4. function str_highlight ($string, $search_str) {
  5. if (trim($string) == '') {
  6. $return_str = 'Empty string';
  7. }
  8. elseif (trim($search_str) == '') {
  9. $return_str = 'Empty search string';
  10. }
  11. else {
  12. // i put style="...." because i don't have a class="highlight" defined
  13. $return_str = preg_replace ("/$search_str/i",'<span class="highlight" style="background:#f00; color:#fff; font-weight:bold; padding:0 2px;">'.$search_str.'</span>',$string);
  14. }
  15. return $return_str;
  16. }
  17. $new_str = str_highlight ($string, $search_str);
  18. echo $new_str;
  19.  
  20. ?>
Reply With Quote  
Join Date: May 2007
Posts: 5
Reputation: aniltc is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
aniltc aniltc is offline Offline
Newbie Poster

Re: search key highlighting ?

  #5  
May 23rd, 2007
Thanks

But multiple keyword is not highliting


***************************
Dani web is super
***************************
Last edited by aniltc : May 23rd, 2007 at 1:09 am.
Reply With Quote  
Join Date: May 2007
Posts: 14
Reputation: Spaiz is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
Spaiz Spaiz is offline Offline
Newbie Poster

Troubleshooting Re: search key highlighting ?

  #6  
May 23rd, 2007
Here's a function I'm using right now:
  1. function hl($c,$q){
  2. $q=explode(' ',str_replace(array('','\\','+','*','?','[','^',']','$','(',')','{','}','=','!','<','>','|',':','#','-','_'),'',$q));
  3. for($i=0;$i<sizeOf($q);$i++)
  4. $c=preg_replace("/($q[$i])(?![^<]*>)/i","<b>\${1}</b>",$c);
  5. return $c;
  6. }

But if you don't have HTML tags in your content you could use a faster function:
  1. function hl($c,$q){
  2. $q=explode(' ',$q);
  3. for($i=0;$i<sizeOf($q);$i++)
  4. $c=preg_replace("/($q[$i])/i","<b>\${1}</b>",$c);
  5. return $c;
  6. }

$content="Simple content. Try it!";
$query="simple t";
echo hl($content,$query); //that should highlight the word "simple" and all the letters "t"
Last edited by Spaiz : May 23rd, 2007 at 10:58 am.
Reply With Quote  
Join Date: May 2007
Posts: 6
Reputation: Lethal.Liquid is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Lethal.Liquid Lethal.Liquid is offline Offline
Newbie Poster

Re: search key highlighting ?

  #7  
May 23rd, 2007
/*
SQL Query Stuff
*/
$it = htmlentities($query);
$it = highlight_string($it);
echo $it;
Last edited by Lethal.Liquid : May 23rd, 2007 at 6:33 pm.
Reply With Quote  
Join Date: Sep 2008
Posts: 1
Reputation: omshankar is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
omshankar omshankar is offline Offline
Newbie Poster

Re: search key highlighting ?

  #8  
Sep 11th, 2008
Hey guys, this is useless
the function is very simple..

But..
If The Paragraph is :

PHP is a better language.

and the search is pHp

then the output will be:

pHp is a better language.

with pHp highlighted
replacing the original word by key, we donwan replacements, huh. only highlight
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 6:36 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC