Dail
Dail

Reputation: 4608

How can I get the exact length of a string in PHP?

I have a strange result with:

strlen("òàùè")

How can I count correctly the string with no-ASCII characters?

I also looked at mb_strlen(), but it does not solve the problem.

Upvotes: 1

Views: 1618

Answers (1)

Gordon
Gordon

Reputation: 317177

mb_strlen will

Return the number of characters in string $str having character encoding $encoding. A multi-byte character is counted as 1.

Your internal encoding is likely not set to UTF-8, so do

echo mb_strlen('òàùè', 'UTF-8');

Or set UTF-8 globally:

mb_internal_encoding('UTF-8');

And then do

echo mb_strlen("òàùè");

will give 4 (demo).

Upvotes: 11

Related Questions