+ -
当前位置:首页 → 问答吧 → 修正msubstr不填加"..."问题

修正msubstr不填加"..."问题

时间:2010-07-28

来源:互联网

以下代码只供大家参考:
  1. function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)
  2. {
  3.         if($suffix){
  4.                 $suffixStr = "...";
  5.         }else{
  6.                 $suffixStr = "";
  7.         }
  8.         if(function_exists("mb_substr")){
  9.                 return mb_substr($str, $start, $length, $charset).$suffixStr;
  10.         }elseif(function_exists('iconv_substr')) {
  11.                 return iconv_substr($str,$start,$length,$charset).$suffixStr;
  12.         }
  13.         $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
  14.         $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
  15.         $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
  16.         $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
  17.         preg_match_all($re[$charset], $str, $match);
  18.         $slice = join("",array_slice($match[0], $start, $length));
  19.         if($suffix) return $slice.$suffixStr;
  20.         return $slice;
  21. }
复制代码

作者: 怀念曾经   发布时间: 2010-07-28

来个沙发,睡觉!

作者: 怀念曾经   发布时间: 2010-07-28

哈哈,修得好,不知tp老大改了没

作者: czd327917086   发布时间: 2011-09-05