zoukankan      html  css  js  c++  java
  • 字符串截取,对数字,英文,汉字都可以

    同长我们在建站的过程中,都会遇到这样的问题,UTF-8编码格式下英文和汉字占用的字节数不同,导致字符串截取过程中会遇到冲突,使用下面这个函数可以完美的解决这个问题。

    function cc_msubstr($str, $length, $start=0, $charset="utf-8", $suffix=true){
            if(function_exists("mb_substr")){
                return mb_substr($str, $start, $length, $charset);
            }elseif(function_exists('iconv_substr')){
                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."..";
            }else{
                return $slice;
            }
     }

  • 相关阅读:
    最小生成树Prim算法
    哈夫曼树与哈夫曼编码
    二叉树的非递归遍历
    浅谈C++中指针和引用的区别
    poj2406 Power Strings
    (收藏)KMP算法的前缀next数组最通俗的解释
    HDU 1556 Color the ball
    Floyd算法
    最短路Dijkstra和Flyod
    编程中无穷大常量的设定技巧
  • 原文地址:https://www.cnblogs.com/beili/p/8962256.html
Copyright © 2011-2022 走看看