Support for Chinese and other coded PHP intercept string function sharing (intercepting Chinese string) _php instance

Source: Internet
Author: User

Simple string interception method that supports Chinese and other encodings, passing the corresponding parameters directly to the call can be

Copy Code code as follows:

/**
* String interception, support Chinese and other coding
* @static
* @access Public
* @param string $STR strings that need to be converted
* @param string $start start position
* @param string $length intercept length
* @param string $charset encoding format
* @param string $suffix truncate display characters
* @return String
*/
function Msubstr ($str, $start =0, $length, $charset = "Utf-8", $suffix =true) {
if (function_exists ("Mb_substr"))
$slice = Mb_substr ($str, $start, $length, $charset);
ElseIf (function_exists (' iconv_substr ')) {
$slice = Iconv_substr ($str, $start, $length, $charset);
if (false = = $slice) {
$slice = ';
}
}else{
$re [' utf-8 '] = "/[\x01-\x7f]| [\XC2-\XDF] [\x80-\xbf]| [\xe0-\xef] [\X80-\XBF] {2}| [\xf0-\xff] [\X80-\XBF] {3}/";
$re [' gb2312 '] = "/[\x01-\x7f]| [\xb0-\xf7] [\xa0-\xfe]/];
$re [' gbk '] = "/[\x01-\x7f]| [\x81-\xfe] [\x40-\xfe]/];
$re [' big5 '] = "/[\x01-\x7f]| [\x81-\xfe] ([\x40-\x7e]|\xa1-\xfe]) /";
Preg_match_all ($re [$charset], $STR, $match);
$slice = Join ("", Array_slice ($match [0], $start, $length));
}
Return $suffix? $slice. ' ... ': $slice;
}

Related Article

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.