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 Link to comment https://forums.phpfreaks.com/topic/54822-solved-bold-array-of-words/ 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; ?> Link to comment https://forums.phpfreaks.com/topic/54822-solved-bold-array-of-words/#findComment-271143 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 Link to comment https://forums.phpfreaks.com/topic/54822-solved-bold-array-of-words/#findComment-271147 Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 Works for me? Link to comment https://forums.phpfreaks.com/topic/54822-solved-bold-array-of-words/#findComment-271149 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; } ?> Link to comment https://forums.phpfreaks.com/topic/54822-solved-bold-array-of-words/#findComment-271151 Share on other sites More sharing options...
chigley Posted June 8, 2007 Share Posted June 8, 2007 My solution worked perfectly though ............ Link to comment https://forums.phpfreaks.com/topic/54822-solved-bold-array-of-words/#findComment-271153 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. Link to comment https://forums.phpfreaks.com/topic/54822-solved-bold-array-of-words/#findComment-271155 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! Link to comment https://forums.phpfreaks.com/topic/54822-solved-bold-array-of-words/#findComment-271156 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.