/**
* 字符串截取,支持中文和其他编码
* @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]);
}

最新文章

  1. listener does not currently know of SID项目部署报数据库错
  2. powerdesigner,eclipse整合安装
  3. 《至少有那天》——IU
  4. 最新 Sublime Text 3 Package Control 安装方法
  5. 如何使一个input文本框随其中内容而变化长度(转)
  6. SVN记住用户名和密码后如何修改
  7. adb服务启动失败处理命令
  8. 查看Linux系统相关版本信息
  9. grpc介绍
  10. PyTorch官方中文文档:torch.Tensor
  11. 从零学习Fluter(八):Flutter的四种运行模式--Debug、Release、Profile和test以及命名规范
  12. LibreOffice字体问题解决;从window复制到Ubuntu
  13. FPGA做正则匹配和网络安全,究竟有多大的优势?
  14. springboot 学习之路 3( 集成mybatis )
  15. git的入门摸索和入门研究
  16. (转)View Transform(视图变换)详解
  17. 使用ejs模板引擎
  18. mysql匹配模式
  19. RHEL6解决无法使用YUM源问题(转)
  20. JDBC Mysql 驱动连接异常

热门文章

  1. 在网页中嵌入Base64编码文件
  2. nodemon运行 提示错误:无法加载文件 C:\Users\gxf\AppData\Roaming\npm\nodemon.ps1,因为在此系统上禁止运行脚本。
  3. INTEL 7代CPU I5 7500 集显HD630 WIN7 64位
  4. linux 下修改时间
  5. git rebase VS git merge
  6. centos7 双网口绑定
  7. [转帖]Nginx rewrite 规则 与 proxy_pass 实现
  8. Elasticsearch配置安装
  9. tp5功能模块添加与调试
  10. Python23之内置函数filter()和map()