* When processing Chinese data, we often need to handle some situations. The following are some functions I have made to address these situations, it has been used in practice * # determine whether a position is the left or right part of a Chinese character, or not a Chinese character # Return value-1 left 0 not a Chinese character 1 right # Usage * & #36; athisis Chinese; printis_chinese (& amp; #36; /*
When processing Chinese data, we often need to handle some situations.
Some of my functions have been used in these cases.
*/
# Determine whether a position is the left or right part of a Chinese character or not a Chinese character
# Return value-1 left 0 is not a Chinese character 1 right
# Usage
/*
$ A = 'This is Chinese ';
Print is_chinese ($ a, 1); // 0
Print is_chinese ($ a, 8); //-1
Print is_chinese ($ a, 9); // 1
*/
Function is_chinese (& $ str, $ location ){
$ Ch = true;
$ I = $ location;
While (ord ($ str [$ I])> 0xa0 & $ I> = 0 ){
$ Ch =! $ Ch;
$ I --;
}
If ($ I! = $ Location ){
$ F_str = $ ch? 1:-1;
}
Else {
$ F_str = false;
}
Return $ f_str;
}
# Chinese string inversion function
# If a string with Chinese characters is reversed using strrev, garbled characters are generated.
/*
Print cstrrev ('this is Chinese'); // si siht in the text
*/
Function cstrrev (& $ str ){
$ Long = strlen ($ str );
For ($ f_str = ', $ chinese = false, $ I = $ long-1; $ I >=0; $ I --){
If (ord ($ str [$ I])> 0xa0 ){
$ Chinese =! $ Chinese;
If ($ chinese = false ){
$ F_str. = $ str [$ I]. $ str [$ I + 1];
}
}
Else {
$ F_str. = $ str [$ I];
}
}
Return $ f_str;
}
/* Chinese string truncation function
Some Chinese string truncation functions often have some problems, such
$ A = "1 medium 2 ";
After two interceptions,
Csubstr ($ str, $ a, 0, 2 );
Csubstr ($ str, $ a, 2, 2)
The loading position may point to the right byte of "medium ".
1, 2
Using this function produces the correct result.
Medium, 2
*/
# Start position, starting from 0
# Long = 0, the string is retrieved from start until the end of the string.
# When ltor is set to true, the characters are obtained from left to right, and when false, the characters are obtained from right to left.
# $ Cn_len the number of Chinese characters in bytes or words. if the number of words is counted, a Chinese character is counted as one byte.
Function csubstr (& $ str, $ start = 0, $ long = 0, $ ltor = true, $ cn_len = 2 ){
If ($ long = 0) $ long = strlen ($ str );
If ($ ltor = false) $ str = cstrrev ($ str );
If ($ cn_len = 1 ){
For ($ I = 0, $ fs = 0; $ I <$ start; $ fs ++)
$ I + = (ord ($ str [$ fs]) <= 0xa0 )? 0.5;
For ($ I = 0, $ fe = $ fs; $ I <$ long; $ fe ++)
$ I + = (ord ($ str [$ fe]) <= 0xa0 )? 0.5;
$ Long = $ fe-$ fs;
}
Else {
$ Fs = (is_chinese ($ str, $ start) = 1 )? $ Start-1: $ start;
$ Fe = $ long + $ start-1;
$ End = (is_chinese ($ str, $ fe) =-1 )? $ Fe-1: $ fe;
$ Long = $ end-$ fs + 1;
}
$ F_str = substr ($ str, $ fs, $ long );
If ($ ltor = false) $ f_str = cstrrev ($ f_str );
Return $ f_str;
}
# Retrieve the left string
# When cn_len = 2, $ long takes the number of words on the left and vice versa.
Function cleft (& $ str, $ long, $ cn_len = 2 ){
$ F_str = csubstr ($ str, 0, $ long, true, $ cn_len );
Return $ f_str;
}
# Retrieving the right string
Function cright (& $ str, $ long, $ cn_len = 2 ){
$ F_str = cstrrev ($ str );
$ F_str = csubstr ($ f_str, 0, $ long, true, $ cn_len );
$ F_str = cstrrev ($ f_str );
Return $ f_str;
}
# Format the branch of an article containing Chinese characters
# There will no longer be any issues caused by line breaks !!!
# Note: each line of the article must be divided by n (chr (13 ).
# $ Width: the number of characters in each line
# $ Br specify the characters used in each line as the Terminator.
Function ctext_wrap (& $ text, $ width = 60, $ br ="
"){