954,224 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Coloring text

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

Pro2000
Posting Whiz
351 posts since Jun 2007
Reputation Points: 44
Solved Threads: 23
 

Surely this is a question to do with html then.
http://www.computerhope.com/htmcolor.htm

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.

Pro2000
Posting Whiz
351 posts since Jun 2007
Reputation Points: 44
Solved Threads: 23
 

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.

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

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.)

kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 195
 

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++;
}
}
?>
kkeith29
Nearly a Posting Virtuoso
1,353 posts since Jun 2007
Reputation Points: 235
Solved Threads: 195
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You