This forum is now read-only. Please use our new forums at discuss.codecademy.com.

Functions, Part I Forum

View Course » View Exercise

848 points
521dd4a4548c35a286002a4e_282904462
Submitted by
Özgür Kılıçaslan
over 3 years ago

strlen() doesn't have UTF8 support?

This doesn't work properly:

echo strlen("Özgür");

echoes 7 instead of 5;


1 vote

permalink

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

454 points
Fb816ef8e77efe98533a183b2d8b7d73?s=140&d=retro
Submitted by
diana_pencheva
over 3 years ago


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/

554 points
F8e548ea62eb9facf4b376b083800bad?s=140&d=retro
Submitted by
Lopera
about 3 years ago


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.

797 points
500f1195e5f9df0002022684_311764658
Submitted by
wynand
over 3 years ago