Use str_split
, explode
or even iterate string will not work if it happens to be using Unicode (UTF-8 for example), as explained in:
For this you can use preg_split
with the modifier u
, thus:
preg_split('//u', $texto, null, PREG_SPLIT_NO_EMPTY);
I used this answer as a basis https://stackoverflow.com/a/25996226/1518921 to generate colors, it should be something like:
<?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 so:
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.
Thank you very much!
– ISABELA DE SOUZA RAMOS DA SILV