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 )!