The Little Guy Posted June 8, 2007 Share Posted June 8, 2007 Anyone know how to make a function that will take in an array of words, and then make those words bold within selected text? So if you were to use this text below, and the array of words were Google and Search all the words google, and search should be bold. Web Images Video News Maps Gmail more Blog Search Blogger Books Calendar Documents Finance Groups Labs Orkut Patents Photos Products Reader Scholar iGoogle | Sign in Google Advanced Search Preferences Language Tools Advertising Programs - Business Solutions - About Google ©20007 Google Quote Link to comment Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 <?php $array = array("Google", "Search"); $string = "Web Images Video News Maps Gmail more Blog Search Blogger Books Calendar Documents Finance Groups Labs Orkut Patents Photos Products Reader Scholar iGoogle | Sign in Google Advanced Search Preferences Language Tools Advertising Programs - Business Solutions - About Google ©20007 Google"; foreach($array as $replace) { $string = str_replace($replace, "<strong>{$replace}</strong>", $string); } echo $string; ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 8, 2007 Author Share Posted June 8, 2007 I tried that, it only hightlights the last array word Quote Link to comment Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 Works for me? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 8, 2007 Author Share Posted June 8, 2007 <?php function boldText($string, $words){ foreach($words as $word){ $str = str_replace($word,'<strong>'.$word.'</strong>',$string); } return $str; } ?> Quote Link to comment Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 My solution worked perfectly though ............ Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 8, 2007 Author Share Posted June 8, 2007 try my function, maybe its that. Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 8, 2007 Author Share Posted June 8, 2007 I renamed some variables, and it works now... Thanks! 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.