Php Chinese string truncation Function

Source: Internet
Author: User
The following two functions are two double-byte string truncation functions, which are used to intercept Chinese strings. The first Chinese Character truncation function is more concise, the latter one is complicated, but more is considered .? Php Tutorial: Chinese string truncation function * the following two functions are two-byte string truncation functions, which are used to intercept Chinese strings.

The following two functions are two double-byte string truncation functions, which are used to intercept Chinese strings. The first Chinese Character truncation function is more concise, the latter one is complicated, but more needs to be considered. ? // Php tutorial Chinese character string truncation function/* the following two functions are two double-byte string truncation functions, that is, the interception of the Chinese character string

The following two functions are two double-byte string truncation functions, which are used to intercept Chinese strings. The first Chinese Character truncation function is more concise, the latter one is complicated, but more needs to be considered.

// Php tutorial Chinese string truncation Function
/*
The following two functions are two double-byte string truncation functions, which are used to intercept Chinese strings. The first Chinese Character truncation function is more concise, the latter one is complicated, but more needs to be considered.
*/

Function substr ($ str = ", $ offset = 0, $ len = 0 ){
$ Len | ($ len = strlen ($ str ));
Preg_match_all ('/./us', $ str, $ result );
Return implode (", array_slice ($ result [0], $ offset, $ len ));
}

// Method 2

If (! Function_exists ('mb _ substr ')){
Function mb_substr ($ str, $ start, $ len = ", $ encoding =" UTF-8 "){
$ Limit = strlen ($ str );

For ($ s = 0; $ start> 0;-$ start) {// found the real start
If ($ s >=$ limit)
Break;

If ($ str [$ s] <= "x7f ")
++ $ S;
Else {
+ $ S; // skip length

While ($ str [$ s]> = "x80" & $ str [$ s] <= "xbf ")
++ $ S;
}
}

If ($ len = ")
Return substr ($ str, $ s );
Else
For ($ e = $ s; $ len> 0;-$ len) {// found the real end
If ($ e >=$ limit)
Break;

If ($ str [$ e] <= "x7f ")
++ $ E;
Else {
++ $ E; // skip length

While ($ str [$ e]> = "x80" & $ str [$ e] <= "xbf" & $ e <$ limit)
++ $ E;
}
}

Return substr ($ str, $ s, $ e-$ s );
}
}

?>

Love Sharing? Php Chinese string truncation Function

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.