/** * php获取字符串中英文混合长度 * @param $str string 字符串 * @param $$charset string 编码 * @return 返回长度,1中文=1位,2英文=1位 */ function strlength($str,$charset=&#39;utf-8&#39;){ if($charset==&#39;utf-8&#39;) $str = iconv(&#39;utf-8&#39;,&#39;gb2312&#39;,$str); $num = strlen($str); $cnnum = 0; for($i=0;$i<$num;$i++){ if(ord(substr($str,$i+1,1))>127){ $cnnum++; $i++; } } $ennum = $num-($cnnum*2); $number = ($ennum/2)+$cnnum; return ceil($number); } //测试输出长度都为15 //开源代码phpfensi.com $str1 = &#39;测试测试测试测试测试测试测试测&#39;; $str2 = &#39;aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&#39;; $str3 = &#39;aa测试aa测试aa测试aa测试aaaaaa&#39;; echo strlength($str1,&#39;gb2312&#39;); echo strlength($str2,&#39;gb2312&#39;); echo strlength($str3,&#39;gb2312&#39;);