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? Thanks!
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? Thanks!
You need to use PHP's multibyte string functions to properly handle Unicode strings:
http://www.php.net/manual/en/ref.mbstring.php
http://www.php.net/manual/en/function.mb-substr.php
You'll also need to specify the character encoding in the
<head>
of your HTML:or:
As previously mentioned in other questions, with PHP, when attempting to get a substring, it doesn't understand multibyte characters (as you get with UTF8 for example).
What the other answers don't mention is that you should hint the encoding you would like to use for the mb_substr
So, for example, I use this:
There are several things you need to consider:
hth
PHP strings doesn't understand multibyte strings by default, the array like indexing will chop of the first byte and if that happen not to be in the ascii range you get this result.
Use mb_substr method.