In PHP, we all know that there are specialized mb_substr and mb_strlen functions that can intercept Chinese characters and calculate the length. However, because these functions are not the core functions of PHP, they are often not enabled. Of course, if you are using your own server, you only need to enable it in php. ini. If you use a virtual host and the server does not enable this function, you need to write a function that suits your national conditions.
The following functions are quite handy. But you must know that it must be used in the UTF-8 environment.
Copy codeThe Code is as follows:
Header ('content-type: text/html; charset = UTF-8 ');
/**
* Functions that can measure the length of a Chinese String
* @ Param $ str string to calculate the length
* @ Param $ type: calculates the length type. 0 (default) indicates that one Chinese character is counted as one character, and 1 indicates that one Chinese character is counted as 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 are all Chinese, ye! ';
$ Len = abslength ($ str );
Var_dump ($ len); // return 12
$ Len = abslength ($ str, '1 ');
Echo '<br/>'. $ len; // return 22
/*
Truncates a Chinese string in UTF-8 encoding. For parameters, refer to the substr function.
@ Param $ str string to be intercepted
@ Param $ start: start position of the part to be intercepted. The negative number indicates reverse truncation.
@ Param $ end the length 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 ));
}
}
}
$ Str2 = 'Wo capture zhong ';
Echo '<br/> ';
Echo utf8_substr ($ str2, 0,-4); // return wo to intercept zhon
Supports gb2312, gbk, UTF-8, and big5 Chinese truncation Methods
Copy codeThe Code is as follows:
/*
* Chinese truncation. Supports gb2312, gbk, UTF-8, and big5
*
* @ Param string $ str string to be intercepted
* @ Param int $ start position
* @ Param int $ length truncation length
* @ Param string $ charset UTF-8 | gb2312 | gbk | big5 Encoding
* @ Param $ whether to add a suffix to 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;
}