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

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.

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

<?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;
 
?>
commented: kudos +5
commented: good +2

Thanks

But multiple keyword is not highliting


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

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"

/*
SQL Query Stuff
*/
$it = htmlentities($query);
$it = highlight_string($it);
echo $it;

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

Thanks Spaiz, that works for me

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.