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

This is UTF-8. For example, 情報 is 2 characters, and ラ リ ー ペ イ ジ is 6 characters.

+3
source share
4 answers

the code

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

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

Result

2
6
+4
source

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

0
source

strlen(utf8_decode('情報'));

0

mb_strlen , , , .

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

, mb_substr, utf8_decode .

0

Source: https://habr.com/ru/post/1727204/


All Articles