我从db获取UTF-8字符串,并尝试回显其第一个字符:
$ title = $ model-> title; echo $ title [0];
我明白了:
怎么了?谢谢!
Bot*_*ázs 29
$first_char = mb_substr($title, 0, 1);
Run Code Online (Sandbox Code Playgroud)
您需要使用PHP的多字节字符串函数来正确处理Unicode字符串:
http://www.php.net/manual/en/ref.mbstring.php
http://www.php.net/manual/en/function.mb-substr.php
您还需要在<head>HTML中指定字符编码:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Run Code Online (Sandbox Code Playgroud)
要么:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-16" />
Run Code Online (Sandbox Code Playgroud)
Pau*_*l S 11
您需要考虑以下几点:
心连心
Lay*_*yke 11
正如之前在其他问题中提到的,使用PHP时,在尝试获取子字符串时,它不了解多字节字符(例如,您使用UTF8).
其他答案没有提到的是你应该提示你想要用于mb_substr的编码
所以,例如,我使用这个:
mb_substr( "Sunday", 0, 1,'UTF8'); // Returns S
mb_substr( "???????????", 0, 1,'UTF8'); // Returns ?
Run Code Online (Sandbox Code Playgroud)