Home > OS >  random string generator with each lines different color
random string generator with each lines different color

Time:01-07

I'm trying to create in PHP a randomized string script, but complex, because each line must have a different color, the color is not important, it can be random, but should not be the same twice. The output must be something like this :

Hello#in blue color
Hello#in red color
Hello#in pink color

i've tried with the str_repeat function,

echo "Colored random String Generator";
echo "<br>";
echo str_repeat("Hello <br>", 100);

but there are too few parameters, i can't really put a <span style="color:$randomColor> between them

Did i miss something ?

CodePudding user response:

You may need to tweak the random color generator as I chucked it together but this is basically what you are after I believe:

function randomColor() {
    return str_pad(dechex(rand(0x000000, 0xFFFFFF)), 6, 0, STR_PAD_LEFT);
}

echo "Colored random String Generator<br>";

$repeatCount = 100;

for ($x = 0; $x <= $repeatCount; $x  ) {
  echo "<span style=\"color: #".randomColor().";\">Hello</span> <br>";
}

CodePudding user response:

Try like this :

echo str_repeat("<span style="color:".$randomColor.">Hello
", 100);

Mark it as solved if this helps or post your entire code in the comments.

  •  Tags:  
  • Related