leachus2002 Posted January 24, 2011 Share Posted January 24, 2011 Hi All, I am trying to create a search system - in which part of it I would like to highlight the matched terminology from the relative $_POST value that is coming from the search box. The SQL that I am using to get the query results is: select * from queue where id like '%$searchvalue%' or subject like '%$searchvalue%' or body like '%$searchvalue%' This returns any rows that include like $searchvalue. So if $searchvalue was say "linux" - I would like the code to highlight the word Linux on the page. I am displaying the results on the page using an MSSQL_FETCH_ARRAY, but only displaying the ID and subject fields. Hope that makes sense? Thanks Matt Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 24, 2011 Share Posted January 24, 2011 you could use str_replace after you get your results like this like this $searchvalue=str_replace("$search" ,"<font color='#FF0000'>$search</font>", $searchvalue); this would then change the color of the variable $search if that represents the word looked for. Quote Link to comment Share on other sites More sharing options...
codefossa Posted January 24, 2011 Share Posted January 24, 2011 Well, for highlight you would prob wanna use <span style="background-color: #FFFF00">$search</span> instead of font color to make it more of a highlight. Quote Link to comment Share on other sites More sharing options...
leachus2002 Posted January 24, 2011 Author Share Posted January 24, 2011 Hi dragon_sa, Thanks for that code - however I am finding that it is highlighting the whole line. Let me post my code: $criteria = $_POST ['search_text']; $search_query = mssql_query("select id,subject,body from table where id like %'$criteria'% or subject like %'$criteria'% or body like %'$criteria'%" $show_results = mssql_fetch_assoc ($search_query ){ $show_results['subject']=str_replace("$show_results['subject']" ,"<font color='#FF0000'>$show_results['subject']</font>", $show_results['subject']); echo $show_results['id']; echo $show_results['subject']; echo $show_results['body']; } Cheers Matt Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 24, 2011 Share Posted January 24, 2011 try this $subject=$show_results['subject']; $subject=str_replace($criteria ,"<font color='#FF0000'>$criteria</font>", $subject); echo $subject; Quote Link to comment Share on other sites More sharing options...
leachus2002 Posted January 24, 2011 Author Share Posted January 24, 2011 Dragon, Awesome! - worked a treat However, I have 1 more question - if I search for "Text" then it will highlight "Text", but not "text". Can this be done? Cheers Matt Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted January 24, 2011 Share Posted January 24, 2011 I made a multi-word multi-color highlighter http://dynaindex.com/color-highlighter.php http://www.phpfreaks.com/forums/php-coding-help/highlight-a-word-using-php/msg1516715/#msg1516715 Is 2 diferent functions there at above link, the one i linked is for when are single values in an array, the other function below it is for when need to explode a line a text for the words, exclude anything, and then create the array. You can look there but here's an even newer version of the code I did. function highlighter($words, $content, $backgroundcolors=null, $fontcolors=null) { preg_match_all('~\w+~', $words, $matched); if(!$matched) { return $content; } $words = str_replace(array('+','-'), "", $words); $words = str_replace(" ", "|", $words); $words = trim($words); $words = explode("|", $words); if(is_null($backgroundcolors) || !is_array($backgroundcolors)) { $backgroundcolors = array('yellow', 'red', 'green', 'orange', 'aqua', 'lightskyblue ', 'pink', 'lime', 'fuchsia', 'maroon', 'navy', 'olive', 'purple', 'gray', 'silver', 'teal', 'grey'); if(is_null($fontcolors) || !is_array($fontcolors)) { $fontcolors = array('black', 'white', 'lime', 'oldlace');//change font colors or add more } } $counting = 0; $numbercolors = max(array_keys($backgroundcolors)); foreach ($words as $word) { $word = preg_quote(trim($word)); $content = preg_replace("/\b($word)\b/i", '<span style="border: solid 2px #FFD700; padding: 1px; -moz-border-radius: 5px; -khtml-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; margin: -0; text-align: justify; line-height: 2px; background-color:'.$backgroundcolors[$counting].';font-family:arial;color:'.$fontcolors[$counting].';">\1</span>', $content); if($counting==$numbercolors){ $counting = 0; } else { $counting++; } } return $content; } ?> a simple usage $post_content = highlighter($search_words, $post_content); You can define the colors also, look at examples the demo or codes other forum post. I had css style in this at first in my included stylesheet, then modified the css class into the output themselves. Any time a css class met another class and was a hyperlink there.....it messed up the hyper, was still a pretty color though, so I redid it with good old depreciated basics of font. Quote Link to comment Share on other sites More sharing options...
dragon_sa Posted January 24, 2011 Share Posted January 24, 2011 this $subject=str_replace($criteria ,"<font color='#FF0000'>$criteria</font>", $subject); would become $subject=str_ireplace($criteria ,"<font color='#FF0000'>$criteria</font>", $subject); this is the case insensitive version Quote Link to comment Share on other sites More sharing options...
leachus2002 Posted January 24, 2011 Author Share Posted January 24, 2011 Dragon! Perfecto! Thank you very much! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.