开发者

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

开发者 https://www.devze.com 2022-12-15 14:52 出处:网络
It is UTF-8. For example, 情开发者_StackOverflow報 is 2 characters while ラリー ペイジ is 6 characters.Code

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

精彩评论

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

关注公众号