2

I've a simple code like so:

echo strlen('Grækenland');

and it's returning 11 instead of expected 10

The server is in denmark, locale was set to danish, but it still returns 11...

user1559555
  • 167
  • 1
  • 1
  • 8
  • Does mb_strlen() give you 10? – Sean Aug 08 '12 at 12:06
  • 2
    Try `echo mb_strlen('Grækenland', 'UTF-8');` – Esailija Aug 08 '12 at 12:06
  • see also: http://stackoverflow.com/questions/571694/what-factors-make-php-unicode-incompatible , https://www.ibm.com/developerworks/library/os-php-unicode/index.html and http://www.joelonsoftware.com/articles/Unicode.html – VolkerK Aug 08 '12 at 12:08

2 Answers2

1

strlen is one of the naïve PHP core functions that understand strings as byte arrays and assume one byte == one character. Use mb_strlen with the correct encoding parameter to actually count characters according to the encoding of your string.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • I'll make an educated guess that UTF-8 is the correct encoding in this case because `æ` is 2 bytes and the other characters are 1 byte long in that encoding, which gives `11`. – Esailija Aug 08 '12 at 12:12
  • Thanks for an explanation, that's much more helpful (as in I won't rely on strlen that much and/or use the correct function instead). – user1559555 Aug 08 '12 at 12:17
  • @user Indeed you do. See my profile for apropos articles if you want to know more about the topic. – deceze Aug 08 '12 at 12:17
0
<?php
  echo mb_strlen('Grækenland', 'utf8');
?>
Peon
  • 7,902
  • 7
  • 59
  • 100