Intercept function. One Chinese Character and one English character.

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.