Use php to intercept a specified length. PHP language: the code used to copy string based on Chinese characters is as follows: functioncutstr ($ string, $ length, $ dot ...) {if (strlen ($ string) $ length) {return $ string ;}$ prechr (1); PHP language: trim strings based on Chinese characters
The code is as follows:
Function cutstr ($ string, $ length, $ dot = '...'){
If (strlen ($ string) <= $ length ){
Return $ string;
}
$ Pre = chr (1 );
$ End = chr (1 );
$ String = str_replace (array ('&', '"', '<', '>'), array ($ pre. '&'. $ end, $ pre. '"'. $ end, $ pre. '<'. $ end, $ pre. '> '. $ end), $ string );
$ Strcut = '';
If (strtolower (CHARSET) = 'utf-8 '){
$ N = $ tn = $ noc = 0;
While ($ n <strlen ($ string )){
$ T = ord ($ string [$ n]);
If ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126 )){
$ Tn = 1;
$ N ++;
$ Noc ++;
} Elseif (194 <=$ t & $ t <= 223 ){
$ Tn = 2;
$ N + = 2;
$ Noc + = 2;
} Elseif (224 <=$ t & $ t <= 239 ){
$ Tn = 3;
$ N + = 3;
$ Noc + = 2;
} Elseif (240 <=$ t & $ t <= 247 ){
$ Tn = 4;
$ N + = 4;
$ Noc + = 2;
} Elseif (248 <=$ t & $ t <= 251 ){
$ Tn = 5;
$ N + = 5;
$ Noc + = 2;
} Elseif ($ t = 252 | $ t = 253 ){
$ Tn = 6;
$ N + = 6;
$ Noc + = 2;
} Else {
$ N ++;
}
If ($ noc >=$ length ){
Break;
}
}
If ($ noc> $ length ){
$ N-= $ tn;
}
$ Strcut = substr ($ string, 0, $ n );
} Else {
For ($ I = 0; $ I <$ length; $ I ++ ){
$ Strcut. = ord ($ string [$ I]) & gt; 127? $ String [$ I]. $ string [++ $ I]: $ string [$ I];
}
}
$ Strcut = str_replace (array ($ pre. '&'. $ end, $ pre. '"'. $ end, $ pre. '<'. $ end, $ pre. '> '. $ end), array ('&', '"', '<', '>'), $ strcut );
$ Pos = strrpos ($ strcut, chr (1 ));
If ($ pos! = False ){
$ Strcut = substr ($ strcut, 0, $ pos );
}
Return $ strcut. $ dot;
}
The trim string code is as follows: function cutstr ($ string, $ length, $ dot = '... ') {if (strlen ($ string) = $ length) {return $ string ;}$ pre = chr (1 );...