使用方括号的字符串中的访问字符无法正确输出
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); // ä
我有一个字符串,我想访问 这是我的代码。 p>
ä code>字符。 但它输出问号而不是正确的字符。 p>
$ array = array('ä','b','c');
$ string ='äbc';
echo $ string; //äbc
echo'&lt; br /&gt;';
echo $ string [0]; //?
echo'&lt; br /&gt;';
echo $ array [0]; //ä
code> pre>
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