views:

144

answers:

1

Hi there!

As we all now, handling multibyte strings is not that easy in PHP. For example I want to get the length of the following string: ä

strlen('ä'); // 2, because ä equals 2 bytes
mb_strlen('ä', 'UTF-8'); // 1
iconv_strlen('ä', 'UTF-8'); // 1

Which functions should I use? The mb_* or iconv_*? Why? Considering that the encoding may not be limited to UTF-8.

Thx in advance!

+2  A: 

Have a look at this Powerpoint presentation:

http://www.nyphp.org/content/presentations/smallworld/April2006-nyphp-Presentation.ppt

In a nutshell: Iconv supports more encodings, but is less portable.

From the presentation:

PHP supports multi byte in two extensions: iconv and mbstring

  • iconv uses an external library (supports more encodings but less portable)
  • mbstring has the library bundled with PHP (less encodings but more portable)
karim79
thx! another pro for mbstring is, that there are a lot more functions like mb_strtoupper, mb_strtolower, etc.
Philippe Gerber