• 自定义方法中英文字符截取


    //方法一:(借鉴于thinkphp中的中英文字符截取)
    
    function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
      if(function_exists("mb_substr"))
        $slice = mb_substr($str, $start, $length, $charset);
      elseif(function_exists('iconv_substr')) {
        $slice = iconv_substr($str,$start,$length,$charset);
        if(false === $slice) {
          $slice = '';
        }
      }else{
        $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));
      }
      return $suffix ? $slice.'...' : $slice;
    }
    
    //方法二:(借鉴于phpcms中的中英文截取)
     
    function str_cut($string, $length, $dot = '...') {
      $strlen = strlen($string);
      if($strlen <= $length) return $string;
      $string = str_replace(array(' ',' ', '&', '"', ''', '', '', '', '<', '>', '·', ''), array('',' ', '&', '"', "'", '', '', '', '<', '>', '·', ''), $string);
      $strcut = '';
      if(strtolower(CHARSET) == 'utf-8') {
        $length = intval($length-strlen($dot)-$length/3);
        $n = $tn = $noc = 0;
        while($n < strlen($string)) {
          $t = ord($string[$n]);
          if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
             $tn = 1; $n++; $noc++;
          } elseif(194 <= $t && $t <= 223) {
             $tn = 2; $n += 2; $noc += 2;
          } elseif(224 <= $t && $t <= 239) {
             $tn = 3; $n += 3; $noc += 2;
          } elseif(240 <= $t && $t <= 247) {
             $tn = 4; $n += 4; $noc += 2;
          } elseif(248 <= $t && $t <= 251) {
             $tn = 5; $n += 5; $noc += 2;
          } elseif($t == 252 || $t == 253) {
             $tn = 6; $n += 6; $noc += 2;
          } else {
             $n++;
          }
          if($noc >= $length) {
             break;
          }
        }
        if($noc > $length) {
          $n -= $tn;
        }
        $strcut = substr($string, 0, $n);
        $strcut = str_replace(array('', '&', '"', "'", '', '', '', '<', '>', '·', ''), array(' ', '&', '"', ''', '', '', '', '<', '>', '·', ''), $strcut);
      } else {
        $dotlen = strlen($dot);
        $maxi = $length - $dotlen - 1;
        $current_str = '';
        $search_arr = array('&',' ', '"', "'", '', '', '', '<', '>', '·', '','');
        $replace_arr = array('&',' ', '"', ''', '', '', '', '<', '>', '·', '',' ');
        $search_flip = array_flip($search_arr);
        for ($i = 0; $i < $maxi; $i++) {
          $current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
          if (in_array($current_str, $search_arr)) {
            $key = $search_flip[$current_str];
            $current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
          }
          $strcut .= $current_str;
        }
      }
      return $strcut.$dot;
    }
  • 相关阅读:
    Element-ui 的 slot 关系理解
    关于Delegate委托和Event事件的学习
    JavaScript 中 prototype 与 __proto__
    正向代理与反向代理的个人理解
    MVC和三层架构
    关于SqlDataAdapter的思考
    关于C#连接Oracle数据库
    关于VS配置环境
    富文本的实现
    博客
  • 原文地址:https://www.cnblogs.com/qhorse/p/4772307.html
Copyright © 2020-2023  润新知