0

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]; // ä

DEMO

Can anyone tell me why?

UPDATED

echo strlen($string); // returns 4
echo mb_substr($string, 0, 1); // ä
Manh Nguyen
  • 930
  • 5
  • 12

2 Answers2

4

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 ä.

Martin
  • 448
  • 4
  • 12
1

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]; // ä

DEMO

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

Community
  • 1
  • 1
Celeritas
  • 14,489
  • 36
  • 113
  • 194