PHP Intercept String function Substr,iconv_substr,mb_substr example and pros and cons Analysis _php tutorial

Source: Internet
Author: User
Example code, using the function substr and ICONV_SUBSTR,MB_SUBSTR

Copy the Code code as follows:


Intercept Chinese string script home _www.jb51.net


$STR = ' 123 Script House 456 welcome everyone's presence ';
Echo substr ($str, 0,4);
Echo '
';
Echo iconv_substr ($str, 0,4, ' utf-8 ');
Echo '
';
Echo mb_substr ($str, 0,4, ' utf-8 ');
?>


Description
The above code uses two functions Iconv_substr and MB_SUBSTR, can be in the current character under the string interception, in order to achieve the truncation of Chinese characters are not garbled.

How should we choose?

1. The Iconv library may not run correctly on some operating systems, and the GNU Extension Library needs to be installed to ensure its proper operation. The MB_SUBSTR function is better compatible.

2, the Iconv function will first convert the current string to the appropriate encoding and then intercept, and the MB function is directly based on the specified encoding interception (provide a secure multi-byte interception), so the MB function is more efficient interception.

Therefore, the MB_SUBSTR function is the most appropriate choice for intercepting Chinese strings.

http://www.bkjia.com/PHPjc/781414.html www.bkjia.com true http://www.bkjia.com/PHPjc/781414.html techarticle sample code, using the function substr and iconv_substr,mb_substr copy code code as follows: HTML Headmeta charset= ' utf-8 ' title intercept Chinese string _ Script home _www.jb51.net/ title/h ...

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