HowdeeDoodee Posted June 9, 2007 Share Posted June 9, 2007 I want to replace/highlight more than one word in a string. How do I do it? The following code works great but it only highlights/replaces one word in the string. Thank you in advance for any replies. <? function highlight_search_criteria($search_results, $search_criteria, $bgcolor='Yellow') { //echo $search_criteria; if (empty($search_criteria)) { return $search_results; } else { $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); } } $term; $term[0] = 'John Doe'; $term[1] = 'Jane Smith'; $term[2] = 'Bill Bobbins'; foreach( $term as $value) { $bgcolor='Yellow'; $start_tag = "<span style='background-color: $bgcolor'>"; $end_tag = '</span>'; $db_result_text = "The users with the most points in this game were John Doe, Jane Smith, and Bill Bobbins."; $result_text = highlight_search_criteria($db_result_text, $value); } //result_text is the sentence or string with a highlighted word echo " result_text = ", $result_text; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54867-solved-how-do-i-replace-more-than-one-word-at-a-time-in-a-string/ Share on other sites More sharing options...
wildteen88 Posted June 9, 2007 Share Posted June 9, 2007 Rather than do a foreach loop outside of the function do it inside the function instead. Just pass your search terms as an array to your highlight_search_criteria function. SO your new code: <?php function highlight_search_criteria($search_results, $search_criteria, $bgcolor='Yellow') { $start_tag = "<span style='background-color: $bgcolor'>"; $end_tag = '</span>'; if(is_array($search_criteria)) { foreach($search_criteria as $term) { $highlighted_results = $start_tag . $term . $end_tag; $search_results = eregi_replace($term, $highlighted_results, $search_results); } } return $search_results; } $term[0] = 'John Doe'; $term[1] = 'Jane Smith'; $term[2] = 'Bill Bobbins'; $db_result_text = "The users with the most points in this game were John Doe, Jane Smith, and Bill Bobbins."; $result_text = highlight_search_criteria($db_result_text, $term); //result_text is the sentence or string with a highlighted word echo " result_text = ", $result_text; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54867-solved-how-do-i-replace-more-than-one-word-at-a-time-in-a-string/#findComment-271362 Share on other sites More sharing options...
soycharliente Posted June 9, 2007 Share Posted June 9, 2007 You were overwriting your returned string every time. You need to check which time it is that you're looping through and use the previously created string (with new background colors added) as the string to search through for the second term. Hope this helps. <?php $string = "<p>The users with the most points in this game were John Doe, Jane Smith, and Bill Bobbins.</p>"; $term = array("John Doe", "Jane Smith", "Bill Bobbins"); $bgcolor = "yellow"; foreach($term as $value) { $count++; if(!empty($value)) { $replaceWith = "<span style=\"background-color:$bgcolor;\">" . $value . "</span>"; if($count==1) { $result = eregi_replace($value, $replaceWith, $string); } else { $result = eregi_replace($value, $replaceWith, $result); } } } echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/54867-solved-how-do-i-replace-more-than-one-word-at-a-time-in-a-string/#findComment-271366 Share on other sites More sharing options...
HowdeeDoodee Posted June 9, 2007 Author Share Posted June 9, 2007 Thank you to both of you. I have been trying to find an answer for this issue for too long. Had a previous thread but no replies. I am below novice level so the help is really, really appreciated. Thank you again. Freaks rule! Quote Link to comment https://forums.phpfreaks.com/topic/54867-solved-how-do-i-replace-more-than-one-word-at-a-time-in-a-string/#findComment-271389 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.