I have a string and i want to access ä
character. But it outputs question mark instead of correct character.
Here is my code.
$array = array('ä', 'b', 'c');
$string = 'äbc';
echo $string; // äbc
echo '<br />';
echo $string[0]; // ?
echo '<br />';
echo $array[0]; // ä
Can anyone tell me why?
UPDATED
echo strlen($string); // returns 4
echo mb_substr($string, 0, 1); // ä
Depending on your charset, the letter ä is a multi-byte letter. When you access a string using array access, it returns the first byte. In case of a multi-byte ä the returns a non printable control character.
Accessing the array using array-access returns the first element, regardless of it's length, in this case the multi-byte ä.
You need to use mb_substr()
like so
$array = array('ä', 'b', 'c');
$string = 'äbc';
echo $string; // äbc
echo '<br />';
echo mb_substr($string, 0, 1, 'UTF8'); // replace UTF8 with whatever charset you are using
echo '<br />';
echo $array[0]; // ä
The reason is because PHP assumes characters take one byte. But this is not the case in your situation with ä so you need to use mb_substr() instead of the index or substr().
I strongly recommend reading the answers to this question Get first character of UTF-8 string