当前位置:首页 > CMS教程 > Thinkphp > 列表

ThinkPHP的截取字符串函数无法显示省略号的解决方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-03-02 15:47:49 浏览: 评论:0 

这篇文章主要介绍了ThinkPHP的截取字符串函数无法显示省略号的解决方法,需要的朋友可以参考下,对于ThinkPHP的截取字符串函数无法显示省略号的情况,解决方法如下:

打开Common/extend.php页面,修改msubstr函数如下:

  1. function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) 
  2. { 
  3.   if(function_exists("mb_substr")) { 
  4.    if($suffix) 
  5.   { 
  6.      if($str==mb_substr($str, $start, $length, $charset))  
  7.      { 
  8.       return mb_substr($str, $start, $length, $charset);  
  9.      } 
  10.      else 
  11.      { 
  12.       return mb_substr($str, $start, $length, $charset)."...";  
  13.      }  
  14.  } 
  15.     else 
  16.  { 
  17.    return mb_substr($str, $start, $length, $charset); 
  18.   } 
  19.   } 
  20.   elseif(function_exists('iconv_substr')) { 
  21.    if($suffix) 
  22.   { 
  23.      if($str==iconv_substr($str,$start,$length,$charset))  
  24.      { 
  25.       return iconv_substr($str,$start,$length,$charset);  
  26.      } 
  27.      else 
  28.      { 
  29.       return iconv_substr($str,$start,$length,$charset)."...";  
  30.      }  
  31.    } 
  32.     else 
  33.  { 
  34.    return iconv_substr($str,$start,$length,$charset); 
  35.   } 
  36.   } 
  37.   $re['utf-8']  = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; 
  38.   $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; 
  39.   $re['gbk']  = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; 
  40.   $re['big5']  = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; 
  41.   preg_match_all($re[$charset], $str, $match); 
  42.   $slice = join("",array_slice($match[0], $start, $length)); 
  43.   if($suffix) return $slice."…"; 
  44.   return $slice; 
  45. } 

Tags: ThinkPHP截取字符串

分享到: