The difference analysis of PHP function substr (), Mb_substr () and Mb_strcut

Source: Internet
Author: User
This article introduces the use and difference of the string interception function in PHP, substr (), Mb_substr () and mb_strcut, and it is necessary for friends to refer to it.

First, look at the substr function. The PHP substr () function is used to split the text, but if you include Chinese characters in the text you want to split, there is a problem. At this point, you can use the Mb_substr/mb_strcut function, mb_substr ()/mb_strcut usage is similar to substr (), except in Mb_substr ()/mb_strcut to add a number of parameters to set the encoding of the string, But the general server did not open Php_mbstring.dll, need to php.ini in the Php_mbstring.dll to open.

Example:

 
      

Output: so that my word

 
      

Output: In this way, mb_substr is to divide characters by word, while mb_strcut splits characters by Byte, but does not produce a half-character phenomenon.

Description of the mbstring function: PHP mbstring Extension Module provides multi-byte character processing ability, usually the most commonly used is to use mbstring to split multibyte characters, so as to avoid the occurrence of half a character, because it is the extension of PHP, It also has a better performance than some custom multibyte-splitting functions. Mbstring extension provides several functions similar to functions, Mb_substr and mb_strcut, to look at the manuals for explanations of them. mb_substrmb_substr () returns the portion of STR specified by the start and length Parameters.mb_substr () performs Multi-Byte Safe substr () operation based on number of characters. Position is counted from the beginning of Str. First character ' s position is 0. Second character position is 1, and so on.

mb_strcutmb_strcut () returns the portion of STR specified by the start and length Parameters.mb_strcut () performs Equivalent operation as MB_SUBSTR () with different method. If start position is multi-byte character ' s second byte or larger, it starts from first byte of multi-byte character. It subtracts string from str to shorter than length and character that's not part of the multi-byte string or not being middle of shift sequence.

Take a look at the following example, there is a text, respectively, with Mb_substr and mb_strcut to do the segmentation:

 
      "Echo" Mb_strcut: ". Mb_strcut ($str, 0, 6, ' utf-8 ');? >

Output: mb_substr: I am a string of comparison mb_strcut: I am

Carefully understand the difference between the three, so that the three functions substr,mb_substr and mb_strcut, applied to the correct situation.

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