Php intercepts a Chinese string function instance. Php interceptions of Chinese character string functions. This article mainly introduces php interceptions of Chinese character string functions. The example analyzes php's skills for Chinese character string operations, note that UTF-8 and gb2312 encoding are converted to php to intercept Chinese character string function instances.
This article mainly introduces the function of intercepting Chinese strings in php. The example analyzes php's skills for Chinese character string operations. pay attention to the conversion between UTF-8 and gb2312 encoding, which is of reference value, for more information, see
This example describes how to use php to intercept Chinese strings. Share it with you for your reference. The specific implementation method is as follows:
The code is as follows:
// Chinese string truncation
Function substr_zh ($ string, $ sublen, $ start = 0, $ code = 'utf-8 '){
If ($ code = 'utf-8 '){
$ Pa = "/[\ x01-\ x7f] | [\ xc2-\ xdf] [\ x80-\ xbf] | \ xe0 [\ xa0-\ xbf] [\ x80 -\ xbf] | [\ xe1-\ xef] [\ x80-\ xbf] [\ x80-\ xbf] | \ xf0 [\ x90-\ xbf] [\ x80- \ xbf] [\ x80-\ xbf] | [\ xf1-\ xf7] [\ x80-\ xbf] [\ x80-\ xbf] [\ x80-\ xbf]/ ";
Preg_match_all ($ pa, $ string, $ t_string );
If (count ($ t_string [0])-$ start> $ sublen ){
Return join ('', array_slice ($ t_string [0], $ start, $ sublen ))."...";
// Array_slice () retrieves a value in the array based on the conditions. The parameter (array, start position, [length])
} Else {
Return join ('', array_slice ($ t_string [0], $ start, $ sublen ));
}
} Else {
$ Start = $ start * 2;
$ Sublen = $ sublen * 2;
$ Strlen = strlen ($ string );
$ Tmpstr = '';
For ($ I = 0; $ I <$ strlen; $ I ++ ){
If ($ I> $ start & $ I <($ start + $ sublen )){
If (ord (substr ($ string, $ I, 1) & gt; 129 ){
// Ord (): returns the ASCII value of the first character of the string.
// Substr (): returns part of the string.
$ Tmpstr. = substr ($ string, $ I, 2 );
} Else {
$ Tmpstr. = substr ($ string, $ I, 1 );
}
}
If (ord (substr ($ string, $ I, 1) & gt; 129 ){
$ I ++;
}
If (strlen ($ tmpstr) <$ strlen ){
$ Tmpstr. = "...";
}
}
Return $ tmpstr;
}
}
$ String = "standing on the roof of a car, getting stuck, staring at the rescue team, and trying to be sleepy, right foot, cute ";
Echo substr_zh ($ string, 10, 0, 'gb2312 ');
?>
I hope this article will help you with php programming.
This article mainly introduces php's function of intercepting Chinese strings. The example analyzes php's skills for Chinese character string operations. pay attention to UTF-8 and gb2312 encoding conversion...