php字符串無亂碼截取函數封裝

/**
 *
 * 中英混合字符串長度判斷
 * @param unknown_type $str
 * @param unknown_type $charset
 */
function strLength($str, $charset = 'utf-8')
{
    if ($charset == 'utf-8'){
        $str = iconv('utf-8', 'gb2312', $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);
}

/**
 *
 * 中英混合的字符串截取
 * @param unknown_type $sourcestr
 * @param unknown_type $cutlength
 */
function cut_str($sourcestr, $cutlength)
{
    $returnstr = '';
    $i = 0;
    $n = 0;
    $str_length = strlen($sourcestr); //字符串的字節數
    while (($n < $cutlength) and ($i <= $str_length)) {
        $temp_str = substr($sourcestr, $i, 1);
        $ascnum = Ord($temp_str); //獲得字符串中第$i位字符的ascii碼
        if ($ascnum >= 224) //若是ASCII位高與224,
        {
            $returnstr = $returnstr . substr($sourcestr, $i, 3); //根據UTF-8編碼規範,將3個連續的字符計爲單個字符
            $i = $i + 3; //實際Byte計爲3
            $n++; //字串長度計1
        } elseif ($ascnum >= 192) //若是ASCII位高與192,
        {
            $returnstr = $returnstr . substr($sourcestr, $i, 2); //根據UTF-8編碼規範,將2個連續的字符計爲單個字符
            $i = $i + 2; //實際Byte計爲2
            $n++; //字串長度計1
        } elseif ($ascnum >= 65 && $ascnum <= 90) //若是是大寫字母,
        {
            $returnstr = $returnstr . substr($sourcestr, $i, 1);
            $i = $i + 1; //實際的Byte數仍計1個
            $n++; //但考慮總體美觀,大寫字母計成一個高位字符
        } else //其餘狀況下,包括小寫字母和半角標點符號,
        {
            $returnstr = $returnstr . substr($sourcestr, $i, 1);
            $i = $i + 1; //實際的Byte數計1個
            $n = $n + 0.5; //小寫字母和半角標點等與半個高位字符寬...
        }
    }
    if ($str_length > $cutlength) {
        $returnstr = $returnstr . "..."; //超過長度時在尾處加上省略號
    }
    return $returnstr;
}

/**
 * PHP實現中文字串截取無亂碼的方法
 * @param $string
 * @param $start
 * @param $length
 * @return string
 */
function GBsubstr($string, $start, $length)
{
    if (strlen($string) > $length) {
        $str = null;
        $len = $start + $length;
        for ($i = $start; $i < $len; $i++) {
            if (ord(substr($string, $i, 1)) > 0xa0) {
                $str .= substr($string, $i, 2);
                $i++;
            } else {
                $str .= substr($string, $i, 1);
            }
        }
        return $str . '...';
    } else {
        return $string;
    }
}

function substr_text($str, $start = 0, $length, $charset = "utf-8", $suffix = "")
{
    if (function_exists("mb_substr")) {
        return mb_substr($str, $start, $length, $charset) . $suffix;
    } elseif (function_exists('iconv_substr')) {
        return iconv_substr($str, $start, $length, $charset) . $suffix;
    }
    $re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("", array_slice($match[0], $start, $length));
    return $slice . $suffix;
}
相關文章
相關標籤/搜索