php中截取UTF-8字符串的函数
来源:互联网 发布:龙治民隐瞒了什么 知乎 编辑:程序博客网 时间:2024/05/12 12:04
截取GB2312/GBK/UTF-8字符编码通用的函数,使用说明:
cut_str(字符串, 截取长度, 开始长度, 编码);
编码默认为 UTF-8,开始长度默认为0。
function cut_str($string, $sublen, $start=0, $code='UTF-8') {
if($code == 'UTF-8') {
$pa = "/[/x01-/x7f]|[/xc2-/xdf][/x80-/xbf]|/xe0[/xa0-/xbf][/x80-/xbf]|[/xe1-/xef][/x80-/xbf][/x80-/xbf]|/xf0[/x90-/xbf][/x80-/xbf][/x80-/xbf]|[/xf1-/xf7][/x80-/xbf][/x80-/xbf][/x80-/xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."…";
return join('', array_slice($t_string[0], $start, $sublen));
}
else {
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i< $strlen; $i++) {
if($i>=$start && $i< ($start+$sublen)) {
if(ord(substr($string, $i, 1))>129) {
$tmpstr.= substr($string, $i, 2);
}
else {
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)< $strlen) $tmpstr.= "…";
return $tmpstr;
}
}
- php中截取UTF-8字符串的函数
- PHP中截取UTF-8中文字符串
- php 截取utf-8字符串
- PHP 截取字符串函数整理(支持gb2312和utf-8)
- PHP 中文字符串截取函数(UTF-8)有效
- PHP截取中英混合字符串(UTF-8)
- 截取UTF-8编码下字符串的函数
- 截取UTF-8编码下字符串的函数
- GBK和Utf-8字符串截取函数
- Yii Framework中截取字符串(UTF-8)的方法
- Yii中截取字符串(UTF-8)的方法
- Yii Framework中截取字符串(UTF-8)的源码
- PHP 截取字符串的函数
- PHP字符串截取函数支持utf和gb2312
- PHP截取UTF-8字符串函数乱码解决方案【绝对好用】
- PHP中英混合字符串截取的函数代码
- php中英混合字符串截取的函数代码
- php中常用的字符串截取函数mb_substr实例解释
- 制作一个简单的文件系统
- 人生
- python简介
- [Object C] 字符串和数组NSString NSArray操作
- 一个人的心态
- php中截取UTF-8字符串的函数
- redhat 终端中文乱码解决方案
- .NET config连接数据库字串配置
- c风格命名和c++命名
- c语言结构体中的冒号的用法
- EBS 錯誤代碼
- LESSON 6 MALWARE part IV
- BlackBerry 蓝牙编程(三)
- Javascript选项卡的实现