PHP string truncation (support Chinese)

Source: Internet
Author: User
<?php/** * String truncation (support Chinese) * @author: Zms * @version: 2011-7-27 *//** * String intercept * @param object $STR: String (Chinese supported) * @param object $start: Intercept start position * @param object $length: Intercept End Position * @param object $encode [OPTIONAL]: string encoding * @param object $input _encode [optional]: The encoding of the input text * @return */function substring ( $str, $start, $length, $encode = ' utf-8 ', $input _encode = ' utf-8 ') {//Encoding conversion $STR = Iconv ($input _encode           , $encode, $STR);           Regular matching Preg_match_all (Getrege ($encode), $str, $match);           The data is obtained from the array, consisting of a string $slice = Join ("", Array_slice ($match [0], $start, $length));       return $slice; }/** * Chinese code * @param object $type * @return * * function Getrege ($type) {$re           GE = ""; Switch (Strtolower ($type)) {case ' utf-8 ': $rege = "/[\x01-\x7f]|[ \xc2-\xdf][\x80-\xbf]| [\xe0-\xef] [\X80-\XBF] {2}| [\xf0-\xff][\x80-\xbf]{3}/";               Break Case ' gb2312 ': $rege = "/[\x01-\x7f]| [\xb0-\xf7]                   [\xa0-\xfe]/];               Break Case ' GBK ': $rege = "/[\x01-\x7f]| [\x81-\xfe]                   [\x40-\xfe]/];               Break Case "Big5": $rege = "/[\x01-\x7f]| [\x81-\xfe] ([\x40-\x7e]|\xa1-\xfe])                   /";               Break                   Default:echo "CharSet error";           Exit       } return $rege; }?>
  • 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.