PHP Chinese string truncation function-Open Source Chinese community-PHP source code

Source: Internet
Author: User
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]);}

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.