note 62320 added to function.iconv-strlen

From: Date: Sat, 25 Feb 2006 00:58:04 +0000
Subject: note 62320 added to function.iconv-strlen
Groups: php.notes 
Request: Send a blank email to php-notes+get-104394@lists.php.net to get a copy of this message
If iconv_strlen is passed a UTF-8 string containing badly formed sequences, it will return FALSE. This is in contrast to mb_strlen of the behaviour of utf8_decode, which strip out any bad sequences; <?php # UTF-8 string containing bad sequence: \xe9 $str = "Iñtërnâtiôn\xe9àlizætiøn"; print "mb_strlen: ".mb_strlen($str,'UTF-8')."\n"; print "strlen/utf8_decode: ".strlen(utf8_decode($str))."\n"; print "iconv_strlen: ".iconv_strlen($str,'UTF-8')."\n"; ?> Displays; mb_strlen: 20 strlen/utf8_decode: 20 iconv_strlen: (PHP 5.0.5) As such it is being "stricter" than mb_strlen and it may mean you need to check for invalid sequences first. A quick way to check is to exploit the behaviour of the PCRE extension (see notes on pattern modifiers); <?php if (preg_match('/^.{1}/us',$str,$ar) != 1) { die("string contains invalid UTF-8"); } ?> A slower but stricter check (regex) can be found at: http://www.w3.org/International/questions/qa-forms-utf-8 Similiar applies to iconv_substr, iconv_strpos and iconv_strrpos ---- Server IP: 194.153.94.11 Probable Submitter: 84.72.36.154 ---- Manual Page -- http://www.php.net/manual/en/function.iconv-strlen.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62320 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62320&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62320&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62320&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62320&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62320&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62320&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62320&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62320&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104394) next »