代碼 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/**做用:統計字符長度包括中文、英文、數字 * 參數:須要進行統計的字符串、編碼格式目前系通通一使用UTF-8 * 時間:2009-07-15 * 修改記錄: $str = "kds"; echo sstrlen($str,'utf-8'); * */ function sstrlen($str,$charset) { $n = 0; $p = 0; $c = ''; $len = strlen($str); if($charset == 'utf-8') { for($i = 0; $i < $len; $i++) { $c = ord($str{$i}); if($c > 252) { $p = 5; } elseif($c > 248) { $p = 4; } elseif($c > 240) { $p = 3; } elseif($c > 224) { $p = 2; } elseif($c > 192) { $p = 1; } else { $p = 0; } $i+=$p;$n++; } } else { for($i = 0; $i < $len; $i++) { $c = ord($str{$i}); if($c > 127) { $p = 1; } else { $p = 0; } $i+=$p;$n++; } } return $n; }