Jump to content

how to replace letters in color from in array.


redarrow

Recommended Posts

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 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]ss

example
[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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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 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!
?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.