Hello everybody;

I want to color a text gradually by PHP

Explain: I want to color each letter with a color, like:

Hello: H=green e=red l=blue (And repeat) l=green o=red

Recommended Answers

All 5 Replies

I think you did not understand me, I explain:
I wrote a chat program , I want when a user sends a message the message appears with mixed colors... the color of the first letter=green , the 2nd letter=red , the 3rd letter=blue and start over the 4th letter=green ,the 5th letter=red the 6th letter=blue and start over the 7th letter=green, 8th letter=red , 9th letter=blue .... until the message's end.

Member Avatar for iamthwee

Honey you do realise the colour is rendered in html? The clever bit, i.e. first letter green second letter red, is done by writing code. Try it.

here i got it. i just wrote the code and lightly tested it. to make it fit with your program i need to now where the string thats being colored is coming from (mysql, ect.)

nevermind about the customizing. i made it into a function. lets say you have words contained in the variable $string, all you would have to do is call the function "color()" with the string that needs to be colored inside the parathesis ex. "color($string)" and the colored text will echoed onto the page.

Heres the color function code:

<?php
function color($str) {
$i = 0;
while ($i < strlen($str)) {
$letters[] = substr($str, $i, 1);
$i++;
}
$colors = array(0 => '<font color="green">',1 => '<font color="red">',2 => '<font color="blue">');
$i = 0;
$c = 0;
while ($i < count($letters)) {
if ($c == 3) {
$c = 0;
}
echo $colors[$c] . $letters[$i] . '</font>';
$i++;
$c++;
}
}
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.