Jump to content


Photo

how to replace letters in color from in array.


  • Please log in to reply
4 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 20 July 2006 - 07:09 AM

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 cheers

this should be the result

hi my name is redarrow i like my motorbike its class

example

$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;


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 07:45 AM

Think I might have gone about this the long way around, but here's what I dreamed up:
<?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;
?>


#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 20 July 2006 - 07:49 AM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 20 July 2006 - 07:55 AM

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.


<?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;
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 20 July 2006 - 08:38 AM

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...
<?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 string

for($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!
?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users