PHP字符串截取,计算字符串长度
2024-08-27 03:15:21
/**
* 字符串截取,支持中文和其他编码
* @param [string] $str [字符串]
* @param integer $start [起始位置]
* @param integer $length [截取长度]
* @param string $charset [字符串编码]
* @param boolean $suffix [是否有省略号]
* @return [type] [description]
*/
function msubstr($str, $start=0, $length=15, $charset="utf-8", $suffix=true) {
if(function_exists("mb_substr")) {
return mb_substr($str, $start, $length, $charset);
} elseif(function_exists('iconv_substr')) {
return iconv_substr($str,$start,$length,$charset);
}
$re['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
$re['gbk'] = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
$re['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
preg_match_all($re[$charset], $str, $match);
$slice = join("",array_slice($match[0], $start, $length));
if($suffix) {
return $slice."…";
}
return $slice;
}
/**
* @计算中文字符串长度,只支持UTF8编码
*/
function utf8_strlen($string = null) {
preg_match_all(“/./us”, $string, $match);
return count($match[0]);
}
最新文章
- listener does not currently know of SID项目部署报数据库错
- powerdesigner,eclipse整合安装
- 《至少有那天》——IU
- 最新 Sublime Text 3 Package Control 安装方法
- 如何使一个input文本框随其中内容而变化长度(转)
- SVN记住用户名和密码后如何修改
- adb服务启动失败处理命令
- 查看Linux系统相关版本信息
- grpc介绍
- PyTorch官方中文文档:torch.Tensor
- 从零学习Fluter(八):Flutter的四种运行模式--Debug、Release、Profile和test以及命名规范
- LibreOffice字体问题解决;从window复制到Ubuntu
- FPGA做正则匹配和网络安全,究竟有多大的优势?
- springboot 学习之路 3( 集成mybatis )
- git的入门摸索和入门研究
- (转)View Transform(视图变换)详解
- 使用ejs模板引擎
- mysql匹配模式
- RHEL6解决无法使用YUM源问题(转)
- JDBC Mysql 驱动连接异常
热门文章
- 在网页中嵌入Base64编码文件
- nodemon运行 提示错误:无法加载文件 C:\Users\gxf\AppData\Roaming\npm\nodemon.ps1,因为在此系统上禁止运行脚本。
- INTEL 7代CPU I5 7500 集显HD630 WIN7 64位
- linux 下修改时间
- git rebase VS git merge
- centos7 双网口绑定
- [转帖]Nginx rewrite 规则 与 proxy_pass 实现
- Elasticsearch配置安装
- tp5功能模块添加与调试
- Python23之内置函数filter()和map()