How to write php Chinese intercept functions mb_strlen and mb_substr
This article introduces the "how to write php Chinese interception function mb_strlen and mb_substr" related knowledge, in the actual case operation process, many people will encounter such a dilemma, then let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
The copy code is as follows:
If (! function_exists ('mb_strlen')) {
Function mb_strlen ($text, $encode) {
If ($encode=='UTF-8') {
Return preg_match_all ('% (?
[\ x09\ x0A\ x0D\ x20 -\ x7e] # ASCII
| | [\ xC2-\ xDF] [\ x80 -\ xBF] # non-overlong 2-byte |
| |\ xE0 [\ xA0-\ xBF] [\ x80 -\ xBF] # excluding overlongs |
| | [\ xE1-\ xEC\ xEE\ xEF] [\ x80 -\ xBF] {2} # straight 3-byte |
| |\ xED [\ x80 -\ x9F] [\ x80 -\ xBF] # excluding surrogates |
| |\ xF0 [\ x90 -\ xBF] [\ x80 -\ xBF] {2} # planes 1-3 |
| | [\ xF1-\ xF3] [\ x80 -\ xBF] {3} # planes 4-15 |
| |\ xF4 [\ x80 -\ x8F] [\ x80 -\ xBF] {2} # plane 16 |
)% xs',$text,$out)
} else {
Return strlen ($text)
}
}
}
/ * from Internet, author unknown * /
If (! function_exists ('mb_substr')) {
Function mb_substr ($str, $start, $len ='', $encoding= "UTF-8") {
$limit = strlen ($str)
For ($s = 0; $start > 0) {/ / found the real start
If ($s > = $limit)
Break
If ($str [$s] = "\ x80" & & $str [$s] 0;-- $len) {/ / found the real end
If ($e > = $limit)
Break
If ($str [$e] = "\ x80" & & $str [$e]