Truncates a string of the specified length.

Source: Internet
Author: User

 

 

/** 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.