HowdeeDoodee Posted June 6, 2007 Share Posted June 6, 2007 I am getting really frustrated trying to find a script to highlight multiple search terms using a boolean search feature on my site. For example, when you type in an AND or OR search request here in Freaks, the result set is highlighted for each of teh search terms you type in. For example, you can type in "highlight" AND "code" and the result set will show you words highlighted. The highlighted words will be "highlight" and "code" without the quotes. I have found very few scripts on the web that are workable. The scripts I have found I do not know how to adapt to my site. My site gets input from an html form. The search variable coming into the php script is $SeeAlso. So if the user wants to search for "dog" AND "muzzle", $SeeAlso will = "dog AND muzzle". I have an explode function that turns $SeeAlso into $terms[0], $terms[1], $terms[2], $terms[3], $terms[4], $terms[5], and $trimmed. $trimmed = all the search terms without any stop words. So a major question now would be is this... Is there any code anywhere that will do what the Freaks board does in highlighting search terms? Where is the code? Can you paste the code into a response to this question? Now, I found the code below but cannot get the code to highlight any terms in the php script I put the code into. I am a novice at php. No, I am below a novice. I do not care what script I use, just so I get one I can make work. Like I said above, I cannot get this code to work. It may be the way I am imputting the variables. If I can get a sample code to work, maybe I can fashion my own script if no scripts are available on the web. This search has been a multi-week effort so I am getting a little frustrated using forums doing what I want done, if you get my drift. Thank you in advance for any replies. <? //if you don't need to strip anything from the string //$terms = explode(" ", str_replace(array("+","-","*","~","\"","(",")","<",">","\"),"",$search))); // highlight search terms function function highlight($buffer) { global $terms; //create an array of colours for highlights $colors = array('80A000','922292','990000','707070','008080','999900','009999','1111FF','FF00FF','808080','008000','006666','800033','000080'); //set $i - this controls which colour is used $i=0; // do the replace for each word in the array foreach($terms as $needle){ //if you run out of colours, start over if ($i>count($colors)-1) { $i=0; } //surround matches with highlight span $buffer = eregi_replace($needle, "<span style='background-color:#$colors[$i]'></span>",$buffer); //increment $i so next colour is different $i++; } //send the results back to output return $buffer; } ?> <? $search = "cows pigs horshes sheep"; //convert search string to array minus operators $terms = explode(" ",$search); ob_start(highlight); echo $terms; echo $terms[0]; echo $terms[1]; echo $terms[2]; ob_end_flush() ?> Quote Link to comment https://forums.phpfreaks.com/topic/54413-code-help-on-this-highlighting-script-or-source-of-a-better-script/ Share on other sites More sharing options...
HowdeeDoodee Posted June 6, 2007 Author Share Posted June 6, 2007 Here is another script I could not get to work. function highlight_search_criteria($search_results, $search_criteria, $bgcolor = 'Yellow') { $start_tag = "<span style=\"background-color: $bgcolor;\">"; $end_tag = '</span>'; if (empty($search_criteria)) { return $search_results; } if (is_array($search_criteria)) { foreach ($search_criteria AS $keyword) { $search_results = str_replace($keyword, "$start_tag . $keyword . $end_tag", $search_results); } } else { $search_results = str_replace($search_criteria, "$start_tag . $search_criteria . $end_tag", $search_results); } return $search_results; } Quote Link to comment https://forums.phpfreaks.com/topic/54413-code-help-on-this-highlighting-script-or-source-of-a-better-script/#findComment-269281 Share on other sites More sharing options...
obsidian Posted June 6, 2007 Share Posted June 6, 2007 Here is another script I could not get to work. Seems like a solid attempt. How are you trying to implement it? Quote Link to comment https://forums.phpfreaks.com/topic/54413-code-help-on-this-highlighting-script-or-source-of-a-better-script/#findComment-269286 Share on other sites More sharing options...
HowdeeDoodee Posted June 6, 2007 Author Share Posted June 6, 2007 Thank you, obsidian, for the inquiry. How I am trying to implement the code may be the problem. I am calling the function as seen below. I have numerous fields in my db. Two of those fields are $Topic and $Subtopic. $Topic = highlight_search_criteria($Topic, $SeeAlso); $Subtopic = highlight_search_criteria($Subtopic, $SeeAlso); Calling the following function with the above calls to $Topic and $Subtopic, does work without any problems. function highlight_search_criteria1($search_results, $search_criteria, $bgcolor='Yellow') { if (empty($search_criteria)) { return $search_results; } elseif { $start_tag = "<span style='background-color: $bgcolor'>"; $end_tag = '</span>'; $highlighted_results = $start_tag . $search_criteria . $end_tag; return eregi_replace($search_criteria, $highlighted_results, $search_results); } } else { } However, using these calls to the function when the function uses "for each" results in no highlighting. I do not have a clue as to what is going on. Any help would be sincerely appreciated. At a minimum, I would like to get the code with "for each" to work anywhere, just so I know the code is good and then I can try and determine where else the problem might be. OR Maybe I could find code elsewhere that is know to be bug free and then I could thest that bug-free code in my app. Thank you again for the response. Quote Link to comment https://forums.phpfreaks.com/topic/54413-code-help-on-this-highlighting-script-or-source-of-a-better-script/#findComment-269498 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.