PHP intercepts a string with Chinese characters, computes Chinese Characters in two bytes, and intercepts the string
<? Phpheader ("Content-type: text/html; charset = UTF-8");/*** truncates a string, which occupies two bytes, the letter occupies one byte * The page encoding must be UTF-8 */function esub ($ str, $ length = 0, $ ext = "... ") {if ($ length <1) {return $ str;} // calculates the string length $ strlen = (strlen ($ str) + mb_strlen ($ str, "UTF-8")/2; if ($ strlen <$ length) {return $ str;} if (mb_check_encoding ($ str, "UTF-8 ")) {$ str = mb_strcut (mb_convert_encoding ($ str, "GBK", "UTF-8"), 0, $ length, "GBK"); $ str = mb_co Nvert_encoding ($ str, "UTF-8", "GBK");} else {return "unsupported document encoding";} $ str = rtrim ($ str ,",.., --- ([,; '"? '<@ "); Return $ str. $ ext ;}$ str =" L say okay to everyone "; var_dump (esub ($ str, 9 ));
Program running result:String'L for everyone ...'(Length = 16)
This function is not supported in gb2312 encoding, because some functions have different outputs in different encoding files. For more information, see this link.