This forum is now read-only. Please use our new forums! Go to forums
strlen() doesn't have UTF8 support?
This doesn’t work properly:
echo strlen("Özgür");
echoes 7 instead of 5;
Answer 52fc94617c82ca40480008f3
1 vote
There is special function mb_strlen http://www.php.net/manual/en/function.mb-strlen.php that deals with encoding.
Answer 5364e75c548c35bce9001d93
1 vote
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/
Answer 5269775df10c608889000556
0 votes
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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency