1 頁 (共 1 頁)

中文切字

發表於 : 2014-05-22 16:38:35
yehlu
http://blog.wu-boy.com/2007/05/php-%E5% ... %E9%A1%8C/

代碼: 選擇全部

// String intercept By Bleakwind
// utf - 8:$byte = 3 | gb2312:$byte = 2 | big5:$byte = 2
function bite_str($string, $start, $len, $byte = 3)
{
    $str     = "";
    $count   = 0;
    $str_len = strlen($string);
    for ($i = 0; $i < $str_len; $i++) {
        if (($count + 1 - $start) > $len) {
            //$str .= "...";
            break;
        }elseif ((ord(substr($string,$i,1)) <= 128) & ($count < $start)) {
            $count++;
        }elseif ((ord(substr($string,$i,1)) > 128) & ($count < $start)) {
            $count = $count + 2;
            $i     = $i + $byte - 1;
        }elseif ((ord(substr($string,$i,1)) <= 128) & ($count >= $start)) {
            $str .= substr($string,$i,1);
            $count++;
        }elseif ((ord(substr($string,$i,1)) > 128) & ($count >= $start)) {
            $str .= substr($string,$i,$byte);
            $count = $count + 2;
            $i     = $i + $byte - 1;
        }
    }
    return $str;
}