前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >thinkphp3.2模板截取字符串函数

thinkphp3.2模板截取字符串函数

作者头像
Sindsun
发布2018-06-14 14:46:20
4880
发布2018-06-14 14:46:20
举报
文章被收录于专栏:狂码一生狂码一生
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)  

{  

    if(function_exists("mb_substr")){  
    
        if($suffix)  
        
              return mb_substr($str, $start, $length, $charset)."...";  
              
        else
        
             return mb_substr($str, $start, $length, $charset);  
             
        }  
        
        elseif(function_exists('iconv_substr')) {  
        
            if($suffix)  
            
                return iconv_substr($str,$start,$length,$charset)."...";  
                
            else
            
                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;

    }

函数解释: //msubstr($str, $start=0, $length, $charset=”utf-8″, $suffix=true)

//$str:要截取的字符串

// $start=0:开始位置,默认从0开始

// $length:截取长度

// $charset=”utf-8″:字符编码,默认UTF-8

// $suffix=true:是否在截取后的字符后面显示省略号,默认true显示,false为不显示

//模版使用:{$vo.title|msubstr=0,5,'utf-8',false}

.pwarm{ text-indent:0px !important; padding: 5px !important; } .pwarm p,a,span{ text-indent:0px !important; word-break: break-all; }

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-05-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档