note 37975 deleted from function.strlen by hholzgra

From: Date: Thu, 02 Aug 2012 12:11:32 +0000
Subject: note 37975 deleted from function.strlen by hholzgra
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-189609@lists.php.net to get a copy of this message
Note Submitter: dtorop932 at hotmail dot com ---- To follow up on dr-strange's utf8_strlen(), here are two succinct alternate versions. The first is slower for multibyte UTF-8, faster for single byte UTF-8. The second should be much faster for all but very brief strings, and can easily live inline in code. Neither validates the UTF-8. Note that the right solution is to use mb_strlen() from the mbstring module, if one is lucky enough to have that compiled in... <?php // choice 1 function utf8_strlen($str) { $count = 0; for ($i = 0; $i < strlen($str); ++$i) { if ((ord($str[$i]) & 0xC0) != 0x80) { ++$count; } } return $count; } // choice 2 function utf8_strlen($str) { return preg_match_all('/[\x00-\x7F\xC0-\xFD]/', $str, $dummy); } ?>

« previous php.notes (#189609) next »