博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php字符串截取函数,支持中文截取
阅读量:7114 次
发布时间:2019-06-28

本文共 933 字,大约阅读时间需要 3 分钟。

/** * 基于PHP的 mb_substr,iconv_substr 这两个扩展来截取字符串,中文字符都是按
1
个字符长度计算; * 该函数仅适用于utf-
8
编码的中文字符串。 * * @
param
$str
原始字符串 * @
param
$length
截取的字符数 * @
param
$append
替换截掉部分的结尾字符串 * @
return
返回截取后的字符串 */
function
sub_str(
$str
,
$length
=
0
,
$append
=
'...'
) {
$str
= trim(
$str
);
$strlength
= strlen(
$str
);
if
(
$length
==
0
||
$length
>=
$strlength
) {
return
$str
; }
elseif
(
$length
<
0
) {
$length
=
$strlength
+
$length
;
if
(
$length
<
0
) {
$length
=
$strlength
; } }
if
( function_exists(
'mb_substr'
) ) {
$newstr
= mb_substr(
$str
,
0
,
$length
,
'utf-8'
); }
elseif
( function_exists(
'iconv_substr'
) ) {
$newstr
= iconv_substr(
$str
,
0
,
$length
,
'utf-8'
); }
else
{ //
$newstr
= trim_right(substr(
$str
,
0
,
$length
));
$newstr
= substr(
$str
,
0
,
$length
); }
if
(
$append
&&
$str
!=
$newstr
) {
$newstr
.=
$append
; }
return
$newstr
; }

本文转自TBHacker博客园博客,原文链接:本文转自TBHacker博客园博客,原文链接:xxxxx,如需转载请自行联系原作者,如需转载请自行联系原作者


你可能感兴趣的文章
我的友情链接
查看>>
基于Metronic的Bootstrap开发框架经验总结(3)--下拉列表Select2插件的使用
查看>>
查看 MySQL 数据库中每个表占用的空间大小
查看>>
Linux登陆图形,佛祖保佑
查看>>
海洋迅雷VIP帐号获取器
查看>>
强制活动目录的站点复制
查看>>
在图形中添加一个图例
查看>>
os 模块 python file 与文件路径
查看>>
nginx 跳转配置
查看>>
VLAN概述
查看>>
我的友情链接
查看>>
部署Cacti监控平台
查看>>
我的友情链接
查看>>
HDU - 2041 - 超级楼梯(dp)
查看>>
α冲刺 (7/10)
查看>>
Autoit 自动化安装软件
查看>>
shell 脚本-----循环数组
查看>>
IBM x系列255服务器系统带宽特性
查看>>
8.23 课程-1 (history Tabb补全 别名 文件名展开{})
查看>>
重写equals方法
查看>>