php无乱码字符串切割

utf8($strLength += 3;)
GB($strLength += 2;)

<?
function cutStr($string, $length) {
    $strcut = '';
    $strLength = 0;
    if(strlen($string) > $length) {
        //将$length换算成实际UTF8格式编码下字符串的长度
        for($i = 0; $i < $length; $i++) {
            if ( $strLength >= strlen($string) )
                break;
            //当检测到一个中文字符时
            if( ord($string[$strLength]) > 127 )
                $strLength += 3;
            else
                $strLength += 1;
        }
        return substr($string, 0, $strLength);
    } else {
        return $string;
    }
}
?>

[日志信息]

该日志于 2012-3-6 10:53 由  paul  发表在 工作日志 网站上,你除了可以发表评论外,还可以转载 "php无乱码字符串切割" 日志到你的网站或博客,但是请保留源地址及作者信息,谢谢!!    (尊重他人劳动,你我共同努力)

Copyright © 2010 - 2025 工作日志 | QQ:285582676 | | 京ICP备15035559号-3

返回顶部