php:根据中文裁减字符串函数方法
define(CHARSET UTF) // 系统默认编码
/**
* 根据中文裁减字符串
* @param $string 字符串
* @param $length 长度
* @param $doc 缩略后缀
* @return 返回带省略号被裁减好的字符串
*/
function cutstr( $string $length $dot = … ) {
if ( strlen( $string ) <= $length ) {
return $string;
}
$pre = chr( )
$end = chr( )
$string = str_replace( array( & < > ) array( $pre & $end $pre $end $pre < $end $pre > $end ) $string )
$strcut = ;
if ( strtolower( CHARSET ) == utf ) {
$n = $tn = $noc = ;
while ( $n < strlen( $string ) ) {
$t = ord( $string[$n] )
if ( $t == || $t == || ( <= $t && $t <= ) ) {
$tn = ;
$n++;
$noc++;
} elseif ( <= $t && $t <= ) {
$tn = ;
$n += ;
$noc += ;
} elseif ( <= $t && $t <= ) {
$tn = ;
$n += ;
$noc += ;
} elseif ( <= $t && $t <= ) {
$tn = ;
$n += ;
$noc += ;
} elseif ( <= $t && $t <= ) {
$tn = ;
$n += ;
$noc += ;
} elseif ( $t == || $t == ) {
$tn = ;
$n += ;
$noc += ;
} else {
$n++;
}
if ( $noc >= $length ) {
break;
}
}
if ( $noc > $length ) {
$n = $tn;
}
$strcut = substr( $string $n )
} else {
for ( $i = ; $i < $length; $i++ ) {
$strcut = ord( $string[$i] ) > ? $string[$i] $string[++$i] : $string[$i];
}
}
$strcut = str_replace( array( $pre & $end $pre $end $pre < $end $pre > $end ) array( & < > ) $strcut )
$pos = strrpos( $strcut chr( ) )
if ( $pos !== false ) {
$strcut = substr( $strcut $pos )
}
return $strcut $dot;
}