954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

search key highlighting ?

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

aniltc
Newbie Poster
5 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

Just use str_replace() to swap out the word with the same word wrapped in a tag that uses CSS to change the background color of the word. Not very hard to do at all.

stymiee
He's No Good To Me Dead
Moderator
3,360 posts since May 2006
Reputation Points: 161
Solved Threads: 38
 

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

aniltc
Newbie Poster
5 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 
<?php
$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';
$search_str = 'php';
function str_highlight ($string, $search_str) {
 if (trim($string) == '') {
  $return_str = 'Empty string';
  }
 elseif (trim($search_str) == '') {
  $return_str = 'Empty search string';
  }
 else {
// i put style="...." because i don't have a class="highlight" defined
  $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);
  }
 return $return_str;
 }
$new_str = str_highlight ($string, $search_str);
echo $new_str;
 
?>
johny_d
Junior Poster in Training
94 posts since May 2007
Reputation Points: 33
Solved Threads: 7
 

Thanks

But multiple keyword is not highliting


***************************
Dani web is super
***************************

aniltc
Newbie Poster
5 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

Here's a function I'm using right now:

function hl($c,$q){
 $q=explode(' ',str_replace(array('','\\','+','*','?','[','^',']','$','(',')','{','}','=','!','<','>','|',':','#','-','_'),'',$q));
 for($i=0;$i<sizeOf($q);$i++)
  $c=preg_replace("/($q[$i])(?![^<]*>)/i","<b>\${1}</b>",$c);
 return $c;
}


But if you don't have HTML tags in your content you could use a faster function:

function hl($c,$q){
 $q=explode(' ',$q);
 for($i=0;$i<sizeOf($q);$i++)
  $c=preg_replace("/($q[$i])/i","<b>\${1}</b>",$c);
 return $c;
}


$content="Simple content. Try it!";
$query="simple t";
echo hl($content,$query); //that should highlight the word "simple" and all the letters "t"

Spaiz
Newbie Poster
20 posts since May 2007
Reputation Points: 14
Solved Threads: 0
 
/*
SQL Query Stuff
*/
$it = htmlentities($query);
$it = highlight_string($it);
echo $it;
Lethal.Liquid
Newbie Poster
6 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

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

omshankar
Newbie Poster
1 post since Sep 2008
Reputation Points: 10
Solved Threads: 0
 

Thanks Spaiz, that works for me

kodegeek
Newbie Poster
2 posts since Mar 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You