(utility) Multiple PHP Chinese string intercept function

Source: Internet
Author: User
String interception is a very common programming task, and often a string interception with Chinese is often used. Although not difficult, but write their own function implementation and time-consuming, here is a more useful string interception function, capable of basic requirements

<?phpfunction syssubstr ($string, $length, $append = False) {if (strlen ($string) <= $length) {return     $string;         } else {$i = 0;             while ($i < $length) {$stringTMP = substr ($string, $i, 1);                 if (Ord ($stringTMP) >=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-a variety of technologies focused on the mainstream of the Internet"; $length = "27"; $append = true; Echo syssubstr ($string, $length, $append); Output//Concise modern magic--special ...? >

Intercept GB2312 Chinese string:

<?php//intercept Chinese string function mysubstr ($str, $start, $len) {    $tmpstr = "";    $strlen = $start + $len;    for ($i = 0; $i < $strlen; $i + +) {        if (Ord (substr ($str, $i, 1)) > 0xa0) {            $tmpstr. = substr ($str, $i, 2);            $i + +;        } else            $tmpstr. = substr ($str, $i, 1);    }    return $tmpstr;}? >

To intercept UTF8 encoded multibyte strings:

<?php//intercepts UTF8 string function utf8substr ($str, $from, $len) {    return preg_replace (' #^ (?: [\x00-\x7f]|[ \xc0-\xff][\x80-\xbf]+) {0, '. $from. '} '.                       ' (?: [\x00-\x7f]| [\xc0-\xff] [\x80-\xbf]+) {0, '. $len. '}). * #s ',                       ' $ ', $str);}? >

UTF-8, GB2312 are supported by the Chinese character interception function:

<?php/* Utf-8, gb2312 both support the Chinese character interception function Cut_str (string, intercept length, start length, encoding);         The encoding defaults to utf-8 start length default to 0 */function cut_str ($string, $sublen, $start = 0, $code = ' UTF-8 ') {if ($code = = ' UTF-8 ') { $pa = "/[\x01-\x7f]| [\XC2-\XDF] [\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]| [\xe1-\xef] [\X80-\XBF] [\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|        [\xf1-\xf7] [\X80-\XBF] [\X80-\XBF]         [\x80-\xbf]/];         Preg_match_all ($pa, $string, $t _string); if (count ($t _string[0])-$start > $sublen) return join ("', Array_slice ($t _string[0], $start, $sublen))."         ...";     return join ("', Array_slice ($t _string[0], $start, $sublen));         } else {$start = $start * *;         $sublen = $sublen;         $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 ($string, $i, 1);         }} if (Ord (substr ($string, $i, 1)) >129) $i + +;         } if (strlen ($TMPSTR) < $strlen) $tmpstr. = "...";     return $tmpstr; }} $str = "ABCD needs to intercept the string"; Echo Cut_str ($STR, 8, 0, ' gb2312 ');?>

The above is (practical article) multiple PHP Chinese string interception function content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.