1<?php 2 3/** 4 * Counts the number of characters of a string in UTF-8. 5 * Unit-tested by Kasper and works 100% like strlen() / mb_strlen() 6 * 7 * @param string UTF-8 multibyte character string 8 * @return integer The number of characters 9 * @see strlen() 10 * @author Martin Kutschker <martin.t.kutschker@blackbox.net> 11 */ 12if (!function_exists('utf8_strlen')) { 13 function utf8_strlen($str) { 14 $n=0; 15 for($i=0; isset($str{$i}) && strlen($str{$i})>0; $i++) { 16 $c = ord($str{$i}); 17 if (!($c & 0x80)) // single-byte (0xxxxxx) 18 $n++; 19 elseif (($c & 0xC0) == 0xC0) // multi-byte starting byte (11xxxxxx) 20 $n++; 21 } 22 return $n; 23 } 24} 25 26?>