This forum is now read-only. Please use our new forums! Go to forums

0 points
over 10 years

strlen() doesn't have UTF8 support?

This doesn’t work properly:

echo strlen("Özgür");

echoes 7 instead of 5;

Answer 52fc94617c82ca40480008f3

1 vote

Permalink

There is special function mb_strlen http://www.php.net/manual/en/function.mb-strlen.php that deals with encoding.

points
Submitted by diana_pencheva
about 10 years

Answer 5364e75c548c35bce9001d93

1 vote

Permalink

Option 1 $string = “José”; echo strlen (utf8_decode($string));

echo returns: 4

Option 2 $string = “tjänster”; echo mb_strlen( $string, ‘utf-8’ );

echo returns: 8

From: http://chrismarslender.com/2013/03/09/phps-strlen-function-sometimes-produces-unexpected-results/

points
Submitted by Lopera
almost 10 years

Answer 5269775df10c608889000556

0 votes

Permalink

I have a hunch that the ‘wrong’ outcome might have something to do with the fact that php thinks Ö and ü are multi-byte characters and counting them as such. See http://www.phpwact.org/php/i18n/charsets for an interesting read about this.

points
Submitted by wynand
over 10 years