参考:

https://www.cnblogs.com/xsphehe/p/5682004.html

示例:

/**
* 参数说明
* $string 欲截取的字符串
* $sublen 截取的长度
* $start 从第几个字节截取,默认为0
* $code 字符编码,默认UTF-8
*/ // $str="哈哈,帅哥在此!";
// echo cut_str($str, 30, 0, 'gb2312'); // gb2312字符编码
// echo cut_str($str,30,0) //utf-8字符编码
// 参考:https://www.cnblogs.com/xsphehe/p/5682004.html
function cut_str($string, $sublen=30, $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;
}
}

最新文章

  1. 正则化方法:L1和L2 regularization、数据集扩增、dropout
  2. Eclipse配置PyDev插件来实现python开发环境
  3. 安装配置Apache
  4. Windows Server Backup 2008 R2 备份Hyper-V
  5. Java [leetcode 10] Regular Expression Matching
  6. 漏洞:WebRTC 泄漏用户IP
  7. Light OJ 1067 Combinations (乘法逆元)
  8. iOS 支持arm_64 和 x86_64 的OpenSSL 静态库(libcrypto.a, libssl.a)
  9. myeclipse 之 快捷键
  10. uartz Spring与Spring Task总结
  11. Vijos 1010 清帝之惑之乾隆
  12. JavaScript实现排序二叉树的相关算法
  13. C# 编写windows服务及服务的安装、启动、删除、定时执行任务
  14. JAVA-比较浮点型数据
  15. Django整合Keras报错:ValueError: Tensor Tensor(&quot;Placeholder:0&quot;, shape=(3, 3, 1, 32), dtype=float32) is not an element of this graph.解决方法
  16. Vue.js的后端数据支持:使用Express建立app, 并使用MongoDB数据库。
  17. 1:4 UI标签和通用标签
  18. python学习笔记之——python模块
  19. leetcode 几何题 位运算 面试编程
  20. lua基础(一)

热门文章

  1. 常用Linq示例代码
  2. pdf转换成word转换器免费版
  3. jsavascript 面向对象的下拉菜单
  4. [RN] 01 - Init: Try a little bit of React Native
  5. iphone弹出窗口效果的制作(Core animation, CALayer)
  6. iOS 开发,工程中混合使用 ARC 和非ARC(转)
  7. ios开发之--调试方法
  8. 【RF库XML测试】Element Attribute Should Be
  9. ubuntu11.10 64bit 编译android 4.0
  10. 判断资源贴图是否有alpha