Search code examples
phpyii

Get first character of UTF-8 string


I get an UTF-8 string from db, and trying to echo its first character:

$title = $model->title;
echo $title[0];

I get:

What's wrong?


Solution

  • There are several things you need to consider:

    1. Check that data in the DB is being stored as UTF-8
    2. Check that the client connection to the DB is in UTF-8 (for example, in mysql see: http://www.php.net/manual/en/mysqli.character-set-name.php)
    3. Make sure that the page has it's content-type set as UTF-8 [you can use header('Content-Type: utf-8'); ]
    4. Try setting the internal encoding, using mb_internal_encoding("UTF-8");
    5. Use mb_substr instead of array index notation