加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP 字符截取 解决中文的截取问题,不用mb系列

发布时间:2020-12-13 05:35:25 所属栏目:PHP教程 来源:网络整理
导读:div class="codetitle" a style="CURSOR: pointer" data="17382" class="copybut" id="copybut17382" onclick="doCopy('code17382')" 代码如下: div class="codebody" id="code17382" function Cut_string($string,$start,$sublen,$extstring='...',$code =

<div class="codetitle"><a style="CURSOR: pointer" data="17382" class="copybut" id="copybut17382" onclick="doCopy('code17382')"> 代码如下:<div class="codebody" id="code17382">
function Cut_string($string,$start,$sublen,$extstring='...',$code = 'UTF-8') {//Cut_string开始
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],$sublen)).$extstring;
return join('',$sublen));
}
else
{
$start = $start2;
$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,2);
}
else
{
$tmpstr.= substr($string,1);
}
}
if(ord(substr($string,1))>129) $i++;
}
if(strlen($tmpstr)<$strlen ) $tmpstr.= $extstring;
return $tmpstr;
}
} //Cut_string结束

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读