/** Function Description: truncates a string of the specified length * UTF-8 special Chinese characters and uppercase letters are counted as 1, other characters are counted as 0.5 ** @ Param string $ STR original string * @ Param int $ Len truncation length * @ Param string $ etc omitted characters... * @ return string the intercepted string */If (! Function_exists ('restrlen') {function restrlen ($ STR, $ Len = 10, $ etc = '... ') {$ restr = ''; $ I = 0; $ n = 0.0; // number of bytes of the string $ strlen = strlen ($ Str ); while ($ n <$ Len) and ($ I <$ strlen) {$ temp_str = substr ($ STR, $ I, 1 ); // obtain the ASCII code of the $ I character in the string $ ascnum = ord ($ temp_str); // If the ASCII bit height is 252 if ($ ascnum> = 252) {// according to the UTF-8 encoding specification, count 6 consecutive characters as a single character $ restr = $ restr. substr ($ STR, $ I, 6); // The actual byte count is 6 $ I = $ I + 6; // string length gauge 1 $ n ++;} elseif ($ Ascnum> = 248) {$ restr = $ restr. substr ($ STR, $ I, 5); $ I = $ I + 5; $ n ++;} elseif ($ ascnum> = 240) {$ restr = $ restr. substr ($ STR, $ I, 4); $ I = $ I + 4; $ n ++;} elseif ($ ascnum> = 224) {$ restr = $ restr. substr ($ STR, $ I, 3); $ I = $ I + 3; $ n ++;} elseif ($ ascnum> = 192) {$ restr = $ restr. substr ($ STR, $ I, 2); $ I = $ I + 2; $ n ++ ;} // elseif ($ ascnum> = 65 and $ ascnum <= 90 and $ ascnum! = 73) {$ restr = $ restr. substr ($ STR, $ I, 1); // the actual number of bytes is still 1 $ I = $ I + 1; // The overall appearance is considered, uppercase letters are counted as a high character $ n ++;} // %, &, @, M, and W. elseif (! (Array_search ($ ascnum, array (37, 38, 64,109,119) === false) {$ restr = $ restr. substr ($ STR, $ I, 1); // the actual number of bytes is still 1 $ I = $ I + 1; // The overall appearance is considered, the note is counted as a high character $ n ++;} // in other cases, including lowercase letters and halfwidth punctuation marks else {$ restr = $ restr. substr ($ STR, $ I, 1); // the actual number of bytes is 1 $ I = $ I + 1; // other lower-case letters, half-width punctuation marks, and half-height characters $ n = $ n + 0.5 ;}} // if ($ I <$ strlen) {$ restr = $ restr. $ etc;} return $ restr ;}}
Truncates a string of the specified length.