php:根據中文裁減字符串函數方法
define(CHARSET
/**
* 根據中文裁減字符串
* @param $string
* @param $length
* @param $doc
* @return 返回帶省略號被裁減好的字符串
*/
function cutstr( $string
if ( strlen( $string ) <= $length ) {
return $string;
}
$pre = chr(
$end = chr(
$string = str_replace( array(
$strcut =
if ( strtolower( CHARSET ) ==
$n = $tn = $noc =
while ( $n < strlen( $string ) ) {
$t = ord( $string[$n] )
if ( $t ==
$tn =
$n++;
$noc++;
} elseif (
$tn =
$n +=
$noc +=
} elseif (
$tn =
$n +=
$noc +=
} elseif (
$tn =
$n +=
$noc +=
} elseif (
$tn =
$n +=
$noc +=
} elseif ( $t ==
$tn =
$n +=
$noc +=
} else {
$n++;
}
if ( $noc >= $length ) {
break;
}
}
if ( $noc > $length ) {
$n
}
$strcut = substr( $string
} else {
for ( $i =
$strcut
}
}
$strcut = str_replace( array( $pre
$pos = strrpos( $strcut
if ( $pos !== false ) {
$strcut = substr( $strcut
}
return $strcut
}
From:http://tw.wingwit.com/Article/program/PHP/201311/20833.html