I need to get an array with all the characters from a word, but the word has letters with special encoding like á, when I execute the follow code:
$word = 'withá';
$word_arr = array();
for ($i=0;$i<strlen($word);$i++) {
$word_arr[] = $word[$i];
}
or
$word_arr = str_split($word);
I get:
array(6) { [0]=> string(1) “w” [1]=> string(1) “i” [2]=> string(1) “t”
[3]=> string(1) “h” [4]=> string(1) “Ô [5]=> string(1) “¡” }
How can I do to obtain each character as follow?
array(5) { [0]=> string(1) “w” [1]=> string(1) “i” [2]=> string(1) “t”
[3]=> string(1) “h” [4]=> string(1) “á” }
Because it is a UTF-8 string, just do
The reason for this is that, even though it looks right in your script, the interpreter converts it into a multibyte character (why
mb_split()works as well). To convert it to proper UTF-8 format, you can use the mb functions or just specifyutf8_decode().