根据中文裁减字符串函数的php代码
来源:互联网 发布:一吻定情知乎 编辑:程序博客网 时间:2024/06/05 06:43
裁减字符串函数有很多,但是根据中文裁减字符串的却很少,在本文将为大家介绍下使用php是如何实现的,感兴趣的朋友不要错过
代码如下:
define(CHARSET, 'UTF-8'); // 系统默认编码
/**
* 根据中文裁减字符串
* @param $string - 字符串
* @param $length - 长度
* @param $doc - 缩略后缀
* @return 返回带省略号被裁减好的字符串
*/
function cutstr( $string, $length, $dot = '...' ) {
if ( strlen( $string ) <= $length ) {
return $string;
}
$pre = chr( 1 );
$end = chr( 1 );
$string = str_replace( array( '&', '"', '<', '>' ), array( $pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end ), $string );
$strcut = '';
if ( strtolower( CHARSET ) == 'utf-8' ) {
$n = $tn = $noc = 0;
while ( $n < strlen( $string ) ) {
$t = ord( $string[$n] );
if ( $t == 9 || $t == 10 || (32 <= $t && $t <= 126) ) {
$tn = 1;
$n++;
$noc++;
} elseif ( 194 <= $t && $t <= 223 ) {
$tn = 2;
$n += 2;
$noc += 2;
} elseif ( 224 <= $t && $t <= 239 ) {
$tn = 3;
$n += 3;
$noc += 2;
} elseif ( 240 <= $t && $t <= 247 ) {
$tn = 4;
$n += 4;
$noc += 2;
} elseif ( 248 <= $t && $t <= 251 ) {
$tn = 5;
$n += 5;
$noc += 2;
} elseif ( $t == 252 || $t == 253 ) {
$tn = 6;
$n += 6;
$noc += 2;
} else {
$n++;
}
if ( $noc >= $length ) {
break;
}
}
if ( $noc > $length ) {
$n -= $tn;
}
$strcut = substr( $string, 0, $n );
} else {
for ( $i = 0; $i < $length; $i++ ) {
$strcut .= ord( $string[$i] ) > 127 ? $string[$i] . $string[++$i] : $string[$i];
}
if ( $pos !== false ) {
$strcut = substr( $strcut, 0, $pos );
}
return $strcut . $dot;
}
代码如下:
define(CHARSET, 'UTF-8'); // 系统默认编码
/**
* 根据中文裁减字符串
* @param $string - 字符串
* @param $length - 长度
* @param $doc - 缩略后缀
* @return 返回带省略号被裁减好的字符串
*/
function cutstr( $string, $length, $dot = '...' ) {
if ( strlen( $string ) <= $length ) {
return $string;
}
$pre = chr( 1 );
$end = chr( 1 );
$string = str_replace( array( '&', '"', '<', '>' ), array( $pre . '&' . $end, $pre . '"' . $end, $pre . '<' . $end, $pre . '>' . $end ), $string );
$strcut = '';
if ( strtolower( CHARSET ) == 'utf-8' ) {
$n = $tn = $noc = 0;
while ( $n < strlen( $string ) ) {
$t = ord( $string[$n] );
if ( $t == 9 || $t == 10 || (32 <= $t && $t <= 126) ) {
$tn = 1;
$n++;
$noc++;
} elseif ( 194 <= $t && $t <= 223 ) {
$tn = 2;
$n += 2;
$noc += 2;
} elseif ( 224 <= $t && $t <= 239 ) {
$tn = 3;
$n += 3;
$noc += 2;
} elseif ( 240 <= $t && $t <= 247 ) {
$tn = 4;
$n += 4;
$noc += 2;
} elseif ( 248 <= $t && $t <= 251 ) {
$tn = 5;
$n += 5;
$noc += 2;
} elseif ( $t == 252 || $t == 253 ) {
$tn = 6;
$n += 6;
$noc += 2;
} else {
$n++;
}
if ( $noc >= $length ) {
break;
}
}
if ( $noc > $length ) {
$n -= $tn;
}
$strcut = substr( $string, 0, $n );
} else {
for ( $i = 0; $i < $length; $i++ ) {
$strcut .= ord( $string[$i] ) > 127 ? $string[$i] . $string[++$i] : $string[$i];
}
}
//unity3d http://www.unitymanual.com/
$strcut = str_replace( array( $pre . '&' . $end, $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中计算中文字符串长度、截取中文字符串的函数代码
- php 截取中文字符串的代码分享
- 支持中文的PHP字符串截取函数
- php 中文字符串截取的函数
- PHP通过字符串调用函数的代码
- PHP中文字符串截取函数
- PHP中文字符串截取函数
- PHP截取中文字符串函数
- php 中文字符串截取函数
- PHP截取含中文的混合字符串长度的函数
- php 中文字符串截取函数-->比较好的
- php函数strrev()反转中文字符串乱码的问题
- php截取中文字符串长度的mb_substr()、mb_strcut()函数,自定义实现中文字符串无乱码方法
- spring和struts2整合入门
- oracle学习笔记
- 黑马程序员---映射类实例(原创)
- java连接oracle 入门odbc
- orcale命令学习笔记
- 根据中文裁减字符串函数的php代码
- 韩顺平oracle视频笔记一
- 韩顺平oracle视频笔记二
- CSharp学习笔记之三 C#中的字符串
- Powershell远程错误整理
- 韩顺平oracle视频笔记三
- 韩顺平oracle视频笔记四
- 事务问题
- 韩顺平oracle视频笔记五