In PHP, we all know that there are special MB_SUBSTR and Mb_strlen functions that can intercept and compute the length of Chinese, but because these functions are not PHP's core functions, they are often not open. Of course, if you are using your own server, just open it in the php.ini. If the use of the virtual host, and the server does not open this function, then we need to write a point for our national conditions of the function to come.
Some of the following functions are quite handy to use. But you know, you have to use it in a utf-8 environment.
Copy Code code as follows:
Header (' Content-type:text/html;charset=utf-8 ');
/**
* Functions that can count the length of a string in Chinese
* @param $str string to compute the length
* @param $type Compute the length type, 0 (default) represents one character in Chinese, 1 for two characters in Chinese
*
*/
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 are all Chinese, ye! ';
$len = Abslength ($STR);
Var_dump ($len); Return 12
$len = Abslength ($str, ' 1 ');
echo ' <br/> '. $len; return 22
/*
Utf-8 code to intercept Chinese strings, parameters can refer to the SUBSTR function
@param $str The string to be intercepted
@param $start The beginning of the intercept, minus the reverse intercept
The length of the @param $end to be intercepted
*/
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 want to intercept Zhongwen ';
echo ' <br/> ';
Echo utf8_substr ($str 2,0,-4); Return wo want to intercept Zhon
Support Gb2312,gbk,utf-8,big5 Chinese interception method
Copy Code code as follows:
/*
* Chinese interception, support Gb2312,gbk,utf-8,big5
*
* @param string $str to intercept
* @param int $start intercept start position
* @param int $length intercept length
* @param string $charset UTF-8|GB2312|GBK|BIG5 encoding
* @param $suffix whether to add a suffix
*/
Public 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;
}