php---截取描述方法
2024-08-21 11:38:51
参考:
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;
}
}
最新文章
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- Eclipse配置PyDev插件来实现python开发环境
- 安装配置Apache
- Windows Server Backup 2008 R2 备份Hyper-V
- Java [leetcode 10] Regular Expression Matching
- 漏洞:WebRTC 泄漏用户IP
- Light OJ 1067 Combinations (乘法逆元)
- iOS 支持arm_64 和 x86_64 的OpenSSL 静态库(libcrypto.a, libssl.a)
- myeclipse 之 快捷键
- uartz Spring与Spring Task总结
- Vijos 1010 清帝之惑之乾隆
- JavaScript实现排序二叉树的相关算法
- C# 编写windows服务及服务的安装、启动、删除、定时执行任务
- JAVA-比较浮点型数据
- Django整合Keras报错:ValueError: Tensor Tensor(";Placeholder:0";, shape=(3, 3, 1, 32), dtype=float32) is not an element of this graph.解决方法
- Vue.js的后端数据支持:使用Express建立app, 并使用MongoDB数据库。
- 1:4 UI标签和通用标签
- python学习笔记之——python模块
- leetcode 几何题 位运算 面试编程
- lua基础(一)
热门文章
- 常用Linq示例代码
- pdf转换成word转换器免费版
- jsavascript 面向对象的下拉菜单
- [RN] 01 - Init: Try a little bit of React Native
- iphone弹出窗口效果的制作(Core animation, CALayer)
- iOS 开发,工程中混合使用 ARC 和非ARC(转)
- ios开发之--调试方法
- 【RF库XML测试】Element Attribute Should Be
- ubuntu11.10 64bit 编译android 4.0
- 判断资源贴图是否有alpha