redarrow Posted March 16, 2008 Share Posted March 16, 2008 i have tried to highlight words and tried every possable way with php and html i want to get the words i want to highlight example the words php is redarrow i have tried all the current php conventions.. in_array <<<<<<<<, eregi <<<<<<<<<,, str_repalce <<<<<<, implode <<<<,, explode <<<<<, loops everythink man how do you do it.......... the result should be the words wanted to highlight as shown but only one word will highlight why? <?php $message='my name is redarrow and i love php'; $a=array("php","is","redarrow"); for($i=0; $i<count($a); $i++){ $y[]=eregi_replace($a[$i],"<font color='red'>$a[$i]</font>",$message); foreach ($y as $result){ $r=$result; } } echo $r; ?> Quote Link to comment https://forums.phpfreaks.com/topic/96442-highlight-words/ Share on other sites More sharing options...
Orio Posted March 16, 2008 Share Posted March 16, 2008 I'd go for something like this... Maybe it can be cleaned up a bit: <?php $str = 'my name is redarrow and i love php'; $words = array("php","is","redarrow"); $words2 = array(); foreach($words as $k=>$v) $words2[$k] = "<font color='red'>".$v."</font>"; echo str_replace($words, $words2, $str); ?> Orio. Quote Link to comment https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493609 Share on other sites More sharing options...
redarrow Posted March 16, 2008 Author Share Posted March 16, 2008 orio that amazing to me ive been three hours here lol.... can you exsplain the empty array........... what it there for.......... $words2 = array();<<< this linking to this $words2[$k] = "<font color='red'>".$v."</font>"; and it using the foreach key why.... Quote Link to comment https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493612 Share on other sites More sharing options...
Orio Posted March 16, 2008 Share Posted March 16, 2008 With preg_replace()... A bit cleaner: <?php $str = 'my name is redarrow and i love php'; $words = array("php","is","redarrow"); $pattern = "/(". implode("|",$words) .")/"; echo preg_replace($pattern, "<font color='red'>$1</font>", $str); ?> First script (with str_replace()), first creates a new array that it's values are the same as those in $words, just they are "wrapped" with the font tags. So then it replaces the matches with the colored words. The second one (preg_replace()), replaces any occurrence of the words with the same word just "wrapped" with the font tags. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493615 Share on other sites More sharing options...
redarrow Posted March 16, 2008 Author Share Posted March 16, 2008 Thank you ill have to study harder well good code cheers.... Quote Link to comment https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493619 Share on other sites More sharing options...
sasa Posted March 16, 2008 Share Posted March 16, 2008 <?php $str = 'my name is redarrow and i love php. I am miss of the world'; $words = array("php","is","redarrow"); //$pattern = "/(". implode("|",$words) .")/"; $pattern = "/\b(". implode("|",$words) .")\b/"; echo preg_replace($pattern, "<font color='red'>$1</font>", $str); ?> Quote Link to comment https://forums.phpfreaks.com/topic/96442-highlight-words/#findComment-493622 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.