Using str_split
, explode
or even iterate string will not work if you are using unicode (UTF-8 for example), as explained in
For this you can use preg_split
with the u
modifier, like this:
preg_split('//u', $texto, null, PREG_SPLIT_NO_EMPTY);
I used this response link to generate the colors, it should look like this:
<?php
function mb_text_color($texto)
{
$letters = preg_split('//u', $texto, null, PREG_SPLIT_NO_EMPTY);
foreach ($letters as &$letter) {
if (trim($letter) === '') continue;
$cor = dechex(rand(0x000000, 0xFFFFFF));
$letter = '<span style="color:#' . $cor . '">' . $letter . '</span>';
}
return implode('', $letters);
}
echo mb_text_color('foo bár');
To use, you can do this:
if (strpos($d5, '{"name":"')) {
echo mb_text_color('LIVE ->' . $a . '|' . $b . ' | C:' . $c);
} else {
echo mb_text_color("DEAD -> $a|$b ");
}
Or so:
if (strpos($d5, '{"name":"')) {
echo 'LIVE ->' . mb_text_color($a . '|' . $b . ' | C:' . $c);
} else {
echo 'DEAD -> ' . mb_text_color("$a|$b ");
Or variable by variable, as you wish.