Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn to intercept functions. One Chinese Character and one English word.
Share a string truncation function written today. It is different from the official one. If you use it, you will know
/**
* Intercept functions. One Chinese Character and one English character.
*/
Function mbsubstr ($ str, $ start = 0, $ length, $ suffix = true, $ charset = "UTF-8 "){
$ 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 );
$ StrArr = $ match [0];
$ Str = array_values (tempStrArr ($ strArr ));
$ StrArrCount = count ($ str );
If ($ strArrCount = $ length + 1 ){
$ Length + = 1;
}
$ Slice = implode ('', array_slice ($ str, $ start, $ length ));
If ($ strArrCount> $ length ){
Return $ suffix? $ Slice. '...': $ slice;
} Else {
Return $ slice;
}
}
Function tempStrArr ($ strArr ){
$ E = range ('A', 'z ');
$ I = count ($ strArr );
Foreach ($ strArr as $ k => $ v ){
If (in_array (strtolower ($ v), $ e )){
If (in_array (strtolower ($ strArr [$ k + 1]), $ e )){
$ StrArr [$ k] = $ v. $ strArr [$ k + 1];
Unset ($ strArr [$ k + 1]);
Break;
}
} Elseif ($ v> 0 & $ v <10 & $ strArr [$ k + 1]> 0 & $ strArr [$ k + 1] <10 ){
$ StrArr [$ k] = $ v. $ strArr [$ k + 1];
Unset ($ strArr [$ k + 1]);
Break;
}
$ I --;
}
If ($ I> 0 ){
$ StrArr = tempStrArr ($ strArr );
}
Return $ strArr;
}
Share this code. I always think that something is wrong. If you have a better idea, please try your best.
For more functions, see http://chajiandaquan.com/function.html
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB