-1

I have strange problem when special chars in loop when add br and use specal chars, if don´t use words with special chars no have problems but when use special chars and use br for show one under other show strange chars

For example if don´t use br :

$text="ála";

for($d=0;$d<strlen($text);$d++)
{

echo $text[$d];
    
}

Result: ála

If use br show strange chars

$text="ála";

for($d=0;$d<strlen($text);$d++)
{

echo $text[$d];
print "<br>";
    
}

Result : � � l a

I don´t know how fix it´s and don´t underetand why happend this issue, howewer here can explain what happend, regards

Fran
  • 1
  • 4
  • 1
    Does this answer your question? [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – M. Eriksson May 14 '22 at 10:14

1 Answers1

0

This is happening because an accented e is part of UTF-8, which are considered "multi-byte" characters. It outputs incorrectly because you're actually splitting the character down the middle and putting <br> in-between. Because you are splitting on the bytes of the string, and not the characters, the computer does not understand that there is an accented e anymore.

If you are using PHP 7.4, you can use the mb_str_split function instead. You need to have the mbstring extension enabled.

If you don't have that enabled, this answer can help you do it without: What is the best way to split a string into an array of Unicode characters in PHP?

Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22