开发者

How to get the numbers of characters in a string in PHP?

It is UTF-8. For example, 情开发者_StackOverflow報 is 2 characters while ラリー ペイジ is 6 characters.


Code

$a = "情報";
$b = "ラリーペイジ";

echo mb_strlen($a, 'UTF-8') . "\n";
echo mb_strlen($b, 'UTF-8') . "\n";

Result

2
6


Use mb_strlen för multibyte character encodings like UTF-8.


You can use strlen(utf8_decode('情報'));


Even if the second argument of mb_strlen is said optional, it is actually needed, even if your internal encoding and your string's one are the same, for portability purposes.

mb_strlen('情報', 'UTF-8');

The same applies for most multi-byte functions, including mb_substr, for wich the utf8_decode option would not work at all.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜