(Practical) multiple Chinese string truncation functions in PHP

Source: Internet
Author: User
String truncation is a very common programming task, which is often used for string truncation with Chinese characters. Although it is not difficult, it takes time to write functions by yourself. here we will introduce a very useful string truncation function to meet basic requirements. string truncation is a very common programming task, string truncation with Chinese characters is often used. Although it is not difficult, it takes time to write a function. here we will introduce a good string truncation function to meet basic requirements.

 = 224) {$ stringTMP = substr ($ string, $ I, 3); $ I = $ I + 3;} elseif (ord ($ stringTMP)> = 192) {$ stringTMP = substr ($ string, $ I, 2); $ I = $ I + 2;} else {$ I = $ I + 1 ;} $ stringLast [] = $ stringTMP;} $ stringLast = implode ("", $ stringLast); if ($ append) {$ stringLast. = "... ";}return $ stringLast ;}$ string =" concise modern magic -- focus on mainstream Internet technologies "; $ length =" 27 "; $ append = true; echo sysSubStr ($ string, $ length, $ appe Nd); // output // concise modern magic -- special...?>

Intercept the GB2312 Chinese string:

 0xa0) {$ tmpstr. = substr ($ str, $ I, 2); $ I ++;} else $ tmpstr. = substr ($ str, $ I, 1);} return $ tmpstr ;}?>

Truncate the UTF-8 encoded multi-byte string:

 

UTF-8, GB2312 support Chinese character truncation function:

 $ Sublen) return join ('', array_slice ($ t_string [0], $ start, $ sublen )). "... "; return join ('', array_slice ($ t_string [0], $ start, $ sublen);} else {$ start = $ start * 2; $ sublen = $ sublen * 2; $ strlen = strlen ($ string); $ tmpstr = ''; for ($ I = 0; $ I <$ strlen; $ I ++) {if ($ I >=$ start & $ I <($ start + $ sublen) {if (ord (substr ($ string, $ I, 1)> 129) {$ tmpstr. = substr ($ string, $ I, 2);} else {$ tmpstr. = substr ($ str Ing, $ I, 1) ;}} if (ord (substr ($ string, $ I, 1)> 129) $ I ++ ;} if (strlen ($ tmpstr) <$ strlen) $ tmpstr. = "... "; return $ tmpstr ;}}$ str =" string to be intercepted by abcd "; echo cut_str ($ str, 8, 0, 'gb2312');?>

The above is (practical) the content of multiple Chinese character string truncation functions in PHP. For more information, see PHP Chinese network (www.php1.cn )!

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.