PHP Chinese string truncation function-jump to the open source Chinese community
/*** Intercept a Chinese string * @ param $ str * @ param int $ start @ start position * @ param $ length @ intercept length * @ param string $ ending @ Terminator *@ return string */private static function stringSubstr ($ str, $ start = 0, $ length, $ ending = '') {// $ str = html_entity_decode ($ str ); // Convert the entity character to html $ str = trim (strip_tags ($ str); // remove the html character $ str = preg_replace ("/\ s | /","", $ str); $ mb_str = mb_substr ($ str, $ start, $ length, 'utf-8'); if ($ length <method: abslength ($ str )) {$ output = $ mb_str. $ ending;} else {$ output = $ mb_str;} return $ output ;} /*** function for counting the length of a Chinese string * @ param $ str @ string to calculate the length * @ return int @ calculate the length type, 0 (default) indicates that a Chinese character is counted as one character, and 1 indicates that a Chinese character is counted as two characters */private static function abslength ($ str) {if (empty ($ str) {return 0 ;} if (function_exists ('MB _ strlen') {return mb_strlen ($ str, 'utf-8');} else {preg_match_all ("/. /u ", $ str, $ ar); return count ($ ar [0]);}