Home  >  Article  >  Backend Development  >  PHP Chinese interception functions mb_strlen and mb_substr written by myself, _PHP tutorial

PHP Chinese interception functions mb_strlen and mb_substr written by myself, _PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:07:26830browse

Written PHP Chinese interception functions mb_strlen and mb_substr,

As we all know, the strlen and substr functions that come with PHP cannot handle Chinese characters, so we will use the mb_ series of functions instead. But what if there is no mbstring library? This requires us to write one ourselves to replace it. Without further ado, let’s start with the code:

Copy code The code is as follows:

if ( !function_exists('mb_strlen') ) {
 function mb_strlen ($text, $encode) {
  if ($encode=='UTF-8') {
   return preg_match_all('%(?:
       [x09x0Ax0Dx20-x7E]           # ASCII
     | [xC2-xDF][x80-xBF]            # non-overlong 2-byte
     |  xE0[xA0-xBF][x80-xBF]       # excluding overlongs
     | [xE1-xECxEExEF][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;--$start) {// found the real start
            if ($s >= $limit)
                break;
 
            if ($str[$s] <= "x7F")
                ++$s;
            else {
                ++$s; // skip length
 
                while ($str[$s] >= "x80" && $str[$s] <= "xBF")
                    ++$s;
            }
        }
 
        if ($len == '')
            return substr($str, $s);
        else
            for ($e = $s; $len > 0; --$len) {//found the real end
                if ($e >= $limit)
                    break;
 
                if ($str[$e] <= "x7F")
                    ++$e;
                else {
                    ++$e;//skip length
 
                    while ($str[$e] >= "x80" && $str[$e] <= "xBF" && $e < $limit)
                        ++$e;
                }
            }
 
        return substr($str, $s, $e - $s);
    }
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955399.htmlTechArticleThe Chinese interception functions mb_strlen and mb_substr written by myself. As we all know, the strlen and substr functions that come with PHP cannot be processed. Chinese characters, so we will use the mb_ series of functions instead...
Statement:
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