|
[U]复制代码[/U] 代码如下:
define(CHARSET, 'UTF-8'); // 系统默认编码
/**
* 根据中文裁减字符串
* @param $string - 字符串
* @param $length - 长度
* @param $doc - 缩略后缀
* @return 返回带省略号被裁减好的字符串
*/
function cutstr( $string, $length, $dot = '...' ) {
if ( strlen( $string ) ' ), array( $pre . '&' . $end, $pre . '"' . $end, $pre . '' . $end ), $string );
$strcut = '';
if ( strtolower( CHARSET ) == 'utf-8' ) {
$n = $tn = $noc = 0;
while ( $n = $length ) {
break;
}
}
if ( $noc > $length ) {
$n -= $tn;
}
$strcut = substr( $string, 0, $n );
} else {
for ( $i = 0; $i 127 ? $string[$i] . $string[++$i] : $string[$i];
}
}
$strcut = str_replace( array( $pre . '&' . $end, $pre . '"' . $end, $pre . '' . $end ), array( '&', '"', '' ), $strcut );
$pos = strrpos( $strcut, chr( 1 ) );
if ( $pos !== false ) {
$strcut = substr( $strcut, 0, $pos );
}
return $strcut . $dot;
}
您可能感兴趣的文章:多个PHP中文字符串截取函数生成随机字符串和验证码的类的PHP实例php截取中文字符串不乱码的方法PHP字符串的连接的简单实例php 字符串压缩方法比较示例PHP preg_replace() 正则替换所有符合条件的字符串php中利用explode函数分割字符串到数组PHP字符串的递增和递减示例介绍php去除字符串换行符示例分享php 判断字符串中是否包含html标签php二维数组转成字符串示例PHP字符串中特殊符号的过滤方法介绍php判断字符串在另一个字符串位置的方法PHP 字符串长度判断效率更高的方法php通过字符串调用函数示例php中3种方法删除字符串中间的空格php截取字符串之截取utf8或gbk编码的中英文字符串示例支持中文和其他编码的php截取字符串函数分享(截取中文字符串)
|
|