zoukankan      html  css  js  c++  java
  • [置顶] 中英文字符串截取

     

    /// <summary>

            /// 
            /// </summary>
            /// <param name="str">要截取的字符串</param>
            /// <param name="num">截取的位数</param>
            /// <returns></returns>
            public string SubStr(string str, int num)
            {
                if (str.Length < num)
                    return str;
                else
                    return str.Substring(0, num)+"...";

            }

    一般这样的话确实能够截取固定位数的字符串,并且在后面加上“...”,但是这根本满足不了实际开发过程当中的要求,不如英文字符要比中文字符段,往往中文字符一大段,而英文字符只有一小截,

    一下摘自网络

    来自于ECSHOP版本(成熟!)

    function sub_str($str, $length = 0, $append = true)

     {

         $str = trim($str);

         $strlength = strlen($str);

         if ($length == 0 || $length >= $strlength)

         {

             return $str;  //截取长度等于0或大于等于本字符串的长度,返回字符串本身

         }

         elseif ($length < 0)  //如果截取长度为负数

         {

             $length = $strlength + $length;//那么截取长度就等于字符串长度减去截取长度

             if ($length < 0)

             {

                 $length = $strlength;//如果截取长度的绝对值大于字符串本身长度,则截取长度取字符串本身的长度

             }

         }

         if (function_exists('mb_substr'))

         {

             $newstr = mb_substr($str, 0, $length, EC_CHARSET);

         }

         elseif (function_exists('iconv_substr'))

         {

             $newstr = iconv_substr($str, 0, $length, EC_CHARSET);

         }

         else

         {

             //$newstr = trim_right(substr($str, 0, $length));

             $newstr = substr($str, 0, $length);

         }

         if ($append && $str != $newstr)

         {

             $newstr .= '...';

         }

          return $newstr;

     }

    来自于THINKPHP版本(成熟!)
    function msubstr($str$start=0, $length$charset="utf-8"$suffix=true) {

    //判断是否开启PHP中的mb_substr()函数

        if(function_exists("mb_substr"))

            $slice mb_substr($str$start$length$charset);

        //如果没有的话对字符进行utf-8字符转化

        elseif(function_exists('iconv_substr')) {

            $slice iconv_substr($str,$start,$length,$charset);

            //转化失败

            if(false === $slice) {

                $slice '';

            }

        }else{

         //utf8类型转化

            $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";

            //gbk类型转化

            $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 && $str != $slice)

         {

            return $slice .= '...';

         }else{

           return $slice;    

         }

    }

    来自于我的版本(方法笨啊)

       /**

       *  author : lihui870920@163.com

       *  date  : 2012-05-12

       *  截取指定的中英文字符的长度

       */

       

      //code支持utf-8 gb2312 两种格式

       

      function cut_str($string, $sublen, $start = 0, $code = 'utf-8')

        {

           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], $start, $sublen))."...";

                    return join('', array_slice($t_string[0], $start, $sublen));

            }else

            {

    $start = $start*2;

    $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, $i, 2);

                    }

                    else

                    {

                        $tmpstr.= substr($string, $i, 1);

                    }

                }

                if(ord(substr($string, $i, 1))>129) $i++;

            }   

        //超出多余的字段就显示...

                if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";

            return $tmpstr;

          }

       }

    来自于IXIS版本(短小精悍!操作很危险!)

    function truncateStr($str, $len, $encoding='utf-8', $end='...') {

    $strLen = mb_strlen($str, $encoding);

    $str = mb_substr($str, 0, $len, $encoding);

    return ( ($strLen > $len) ? ($str . $end) : $str );

    }


  • 相关阅读:
    Bayan 2015 Contest Warm Up D. CGCDSSQ 暴力
    Codeforces Round #361 (Div. 2) D. Friends and Subsequences RMQ+二分
    Educational Codeforces Round 21 D. Array Division 前缀和
    Educational Codeforces Round 23 E. Choosing The Commander Trie
    Educational Codeforces Round 23 D. Imbalanced Array 单调栈
    Codeforces Round #421 (Div. 1) B. Mister B and PR Shifts 模拟
    Educational Codeforces Round 24 E. Card Game Again 二分+线段树
    Educational Codeforces Round 25 E. Minimal Labels 优先队列
    Codeforces Round #426 (Div. 1) B. The Bakery DP+线段树
    Codeforces Round #407 (Div. 1) C. The Great Mixing 背包DP+Bitset
  • 原文地址:https://www.cnblogs.com/wsq724439564/p/3258200.html
Copyright © 2011-2022 走看看