Home > php教程 > php手册 > 截取函数,中文一个,英文两个算一个

截取函数,中文一个,英文两个算一个

WBOY
Release: 2016-06-07 11:43:32
Original
1639 people have browsed it

截取函数,中文一个,英文两个算一个
共享一个今天写的截取字符串函数,跟官方的不一样,用了你就知道/**<br> * 截取函数,中文一个,英文两个算一个<br> */<br> function mbsubstr($str, $start=0, $length, $suffix=true, $charset="utf-8") {<br>     $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";<br>     $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";<br>     $re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";<br>     $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";<br>     preg_match_all($re[$charset], $str, $match);<br>     <br>     $strArr = $match[0];<br>     $str = array_values(tempStrArr($strArr));<br>     $strArrCount = count($str);<br>     if($strArrCount == $length+1){<br>         $length += 1;<br>     }<br>     <br>     $slice = implode('',array_slice($str,$start,$length));<br>     if($strArrCount > $length){<br>         return $suffix ? $slice.'...' : $slice;<br>     }else{<br>         return $slice;<br>     }<br> }<br> function tempStrArr($strArr){<br>     $e = range('a','z');<br>     $i = count($strArr);<br>     foreach($strArr as $k=>$v){<br>         if(in_array(strtolower($v), $e)){<br>             if(in_array(strtolower($strArr[$k+1]), $e)){<br>                 $strArr[$k] = $v.$strArr[$k+1];<br>                 unset($strArr[$k+1]);<br>                 break;<br>             }<br>         }elseif($v > 0 && $v  0 && $strArr[$k+1]              $strArr[$k] = $v.$strArr[$k+1];<br>             unset($strArr[$k+1]);<br>             break;<br>         }<br>         $i--;<br>     }<br>     if($i > 0){<br>         $strArr = tempStrArr($strArr);<br>     }<br>     <br>     return $strArr;<br> }共享这个代码。我总觉得哪里不对劲,有更好的思路的大神求思路



更多函数参见 http://chajiandaquan.com/function.html

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template