redarrow Posted July 20, 2006 Share Posted July 20, 2006 how to get any letter chage color in an array no mattter what color the letter.in thory all the array letters should change all the word letters.but it dosent please help cheersthis should be the resulthi my n[color=red]a[/color]me is red[color=red]a[/color]rrow i like my motor[color=red]b[/color]ike its [color=red]c[/color]l[color=red]a[/color]ssexample[code]$word="hi my name is redarrow i like my motorbike its class";$arr=array("a","b","c");$letters=implode("",$arr);$result=str_replace($letters,"<font color='red'>$letters</font>",$word);echo $result;[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15111-how-to-replace-letters-in-color-from-in-array/ Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 Think I might have gone about this the long way around, but here's what I dreamed up:[code]<?php$str = "hi my name is redarrow i like my motorbike its class";$tmp = array();for($x=0; $x<strlen($str); $x++) { $tmp[] = $str{$x};}$letters = array("a","b","c");$new = "";foreach($tmp as $l) { if(in_array($l,$letters)) { $new .= "<span style='color:#ff0000'>$l</span>"; } else { $new .= $l; }}echo $new;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15111-how-to-replace-letters-in-color-from-in-array/#findComment-60872 Share on other sites More sharing options...
redarrow Posted July 20, 2006 Author Share Posted July 20, 2006 nice but i need the array a-z ok then each letter a-z a diffrent collor but can not do it please help and cheers. Quote Link to comment https://forums.phpfreaks.com/topic/15111-how-to-replace-letters-in-color-from-in-array/#findComment-60873 Share on other sites More sharing options...
redarrow Posted July 20, 2006 Author Share Posted July 20, 2006 please brake this down into baby terms as i want to understand every last deatail please cheers.and thank you so much cheers looks hard to me.[code]<?php// this assign the sentence understand that.$str = "hi my name is redarrow i like my motorbike its class";// dont get this ?$tmp = array();// for loop but with strlen to do what ?for($x=0; $x<strlen($str); $x++) {// dont understand at all $tmp[] = $str{$x};}// get that$letters = array("a","b","c");// what this for?$new = "";foreach($tmp as $l) { if(in_array($l,$letters)) { $new .= "<span style='color:#ff0000'>$l</span>"; } else { $new .= $l; }}echo $new;?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15111-how-to-replace-letters-in-color-from-in-array/#findComment-60874 Share on other sites More sharing options...
zq29 Posted July 20, 2006 Share Posted July 20, 2006 Ok, slightly modified version, this one colours slected letters different colours. Plus I took out some stuff that wasn't really neccesary. Commented this time...[code]<?php$str = "the quick brown fox jumped over the lazy dog"; //The string!//Option to auto generate an array of letters/colours//$letters = array();//foreach(range("a","z") as $l) $letters[$l] = "#".str_pad(dechex(rand(0,255)),2,"0",STR_PAD_LEFT).str_pad(dechex(rand(0,255)),2,"0",STR_PAD_LEFT).str_pad(dechex(rand(0,255)),2,"0",STR_PAD_LEFT);$letters = array("a" => "#ff0000","b" => "#00ff00","c" => "#0000ff"); //Our array of letters and colours$new = ""; //This will hold our final stringfor($x=0; $x<strlen($str); $x++) { //Loop through our string letter by letter $l = $str{$x}; if(array_key_exists($l,$letters)) { //If the current letter in the string exists in the letter/colour array, colour it. $new .= "<span style='color:$letters[$l]'>$l</span>"; } else { $new .= $l; //If it doesn't exist, just print it normally }}echo $new; //Echo our new coloured string!?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/15111-how-to-replace-letters-in-color-from-in-array/#findComment-60879 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.