Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1970 | Replies: 7
![]() |
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
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
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
•
•
Join Date: May 2006
Location: New Jersey
Posts: 1,422
Reputation:
Rep Power: 5
Solved Threads: 34
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!
Brainyminds | Merchant Account Services | I Love Code
IT'S HERE: Merchant Accounts 101 Everything you need to know about merchant accounts!
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks
This is what i have done so far
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
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
•
•
Join Date: May 2007
Location: Bucharest, RO
Posts: 67
Reputation:
Rep Power: 2
Solved Threads: 4
php Syntax (Toggle Plain Text)
<?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; ?>
•
•
Join Date: May 2007
Posts: 5
Reputation:
Rep Power: 0
Solved Threads: 0
Thanks
But multiple keyword is not highliting
***************************
Dani web is super
***************************
But multiple keyword is not highliting
***************************
Dani web is super
***************************
Last edited by aniltc : May 23rd, 2007 at 1:09 am.
•
•
Join Date: May 2007
Posts: 14
Reputation:
Rep Power: 2
Solved Threads: 0
Here's a function I'm using right now:
But if you don't have HTML tags in your content you could use a faster function:
$content="Simple content. Try it!";
$query="simple t";
echo hl($content,$query); //that should highlight the word "simple" and all the letters "t"
PHP Syntax (Toggle Plain Text)
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:
PHP Syntax (Toggle Plain Text)
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"
Last edited by Spaiz : May 23rd, 2007 at 10:58 am.
•
•
Join Date: May 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
/* SQL Query Stuff */ $it = htmlentities($query); $it = highlight_string($it); echo $it;
Last edited by Lethal.Liquid : May 23rd, 2007 at 6:33 pm.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode