In PHP, we all know that there are special MB_SUBSTR and Mb_strlen functions that can intercept and calculate the length of Chinese, but since these functions are not the core functions of PHP, they are often not open. Of course, if you are using your own server, just open it in php.ini. If the virtual host is used, and the server does not have the function to open it, then we need to write out some of our own functions.
Here are some of the functions that are quite handy. But you know, you have to use it in a utf-8 environment.
Header (' Content-type:text/html;charset=utf-8 '); /** * Can count Chinese string length function * @param $str string to calculate length * @param $type calculate length type, 0 (default) means one character in Chinese, 1 means a Chinese count two characters * */function Abslength ($ STR) {if (empty ($STR)) {return 0;} if (Function_exists (' Mb_strlen ')) {return Mb_strlen ($str, ' utf-8 ');} else {Preg_match_ All ("/./u", $str, $ar); return count ($ar [0]); }} $str = ' We're all Chinese, ye!. '; $len = Abslength ($STR); Var_dump ($len); return $len = Abslength ($str, ' 1 '); echo ' <br/> '. $len; return */* UTF-8 encoding to intercept the Chinese string, parameters can refer to the SUBSTR function @param $str to intercept the string @param $start to intercept the beginning of the position, negative for the reverse intercept @param $end to intercept the length */function Utf8_substr ($STR, $start =0) {if (empty ($STR)) {return false;} if (Function_exists (' mb_substr ')) {if (func_num _args () >= 3) {$end = Func_get_arg (2), return Mb_substr ($str, $start, $end, ' Utf-8 '),} else {mb_internal_encoding ("utf- 8 "); Return Mb_substr ($str, $start); }} else {$null = ""; Preg_match_all ("/./u", $str, $ar); if (Func_num_args () >= 3) {$end = Func_get_arg (2); return Join ($null, Array_slice ($ar [0], $start, $end)); } else {return join ($null, array_slice ($ar [0], $start));}} } $str 2 = ' wo to intercept Zhongwen '; echo ' <br/> '; Echo utf8_substr ($str 2,0,-4); Return wo to intercept Zhon
Support Gb2312,gbk,utf-8,big5 Chinese interception method
<?php/* * Chinese intercept, support gb2312,gbk,utf-8,big5 * * @param string $STR the string to intercept * @param int $start Intercept start position * @param int $length intercept length * @param string $charset UTF-8|GB2312|GBK|BIG5 encoded * @param $suffix suffix */publi C function Csubstr ($STR, $start =0, $length, $charset = "Utf-8", $suffix =true) {if (Function_exists ("Mb_substr")) {if (mb_ Strlen ($str, $charset) <= $length) return $str; $slice = Mb_substr ($str, $start, $length, $charset); } else {$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); if (count ($match [0]) <= $length) return $str; $slice = Join ("", Array_slice ($match [0], $start, $length));} if ($suffix) return $slice. " ..."; return $slice; }
The above is (practical article) in PHP to calculate the length of the Chinese string, the function code to intercept Chinese strings, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!