Php 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 is complicated, but the following two functions are two double-byte string truncation functions, which are used to intercept Chinese strings. well, 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 );
}
}
?>
...