Steven
Steven

Reputation: 25274

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

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

Upvotes: 3

Views: 201

Answers (4)

Benoît Vidis
Benoît Vidis

Reputation: 3902

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.

Upvotes: 0

Peter Lindqvist
Peter Lindqvist

Reputation: 10200

Code

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

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

Result

2
6

Upvotes: 4

Question Mark
Question Mark

Reputation: 3606

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

Upvotes: 0

Emil Vikström
Emil Vikström

Reputation: 91892

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

Upvotes: 0

Related Questions