zoukankan      html  css  js  c++  java
  • php 无乱码截取中文

    今天有个朋友问我,无乱码截取中文的,我找了找以前的代码,现在po出来,自己看看吧

    
    
     1 function msubstr($str, $encodeType='utf-8',$start = 0, $length = 20, $hasSuffix = false, $suffix = '。。。') {
     2     $res['utf-8'] = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/"; 
     3     $res['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/"; 
     4     $res['gbk']= "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/"; 
     5     $res['big5'] = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/"; 
     6     if(!array_key_exists($encodeType, $res)){
     7         return false;
     8     }
     9     $re=$res[$encodeType];
    10     preg_match_all ( $re, $str, $match );
    11     $slice = join ( "", array_slice ( $match [0], $start, $length ) );
    12     if ($hasSuffix) {
    13         return $slice . $suffix;
    14     } else {
    15         return $slice;
    16     }
    17 }
    18 $str='我们啥呢好的的司法考试多拉风了的司法考试多拉风了的司法考试多拉风了司法考试多拉风了';
    19 $encodeType='utf-3';
    20 echo msubstr($str,$encodeType);
    
    
    
    
    
  • 相关阅读:
    netty 服务端 启动阻塞主线程
    idea踩过的坑
    bat批量重命名
    图片上传
    TCP/IP入门指导
    CPU governor调节器汇总
    IT咨询顾问:一次吐血的项目救火
    python 数组
    Python字符串
    基于Python实现对各种数据文件的操作
  • 原文地址:https://www.cnblogs.com/zhangjun516/p/3021970.html
Copyright © 2011-2022 走看看