Cut the php code of the string function based on Chinese characters. Copy the code as follows: define (CHARSET, UTF-8 ); the default encoding is *** used to cut the string according to Chinese characters * @ param $ string-string * @ param $ length-length * @ param $ doc-shrink
The code is as follows:
Define (CHARSET, 'utf-8'); // default system encoding
/**
* Trim strings based on Chinese characters
* @ Param $ string-string
* @ Param $ length-length
* @ Param $ doc-Thumbnail suffix
* @ Return returns the string with the ellipsis truncated.
*/
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 http://www.bkjia.com/PHPjc/621703.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/621703.htmlTechArticle code is as follows: define (CHARSET, 'utf-8 '); // default system encoding/*** cut the string according to Chinese characters * @ param $ string-string * @ param $ length-length * @ param $ doc-shrink...