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