The mb_substr of PHP string splitting function

Source: Internet
Author: User
PHP has a lot of ways of handling strings , and tonight we've chosen a very representative and widely used function to explain that the word

character string Split function:mb_substr (), I hope you will recognize really learn Learning Comprehension!

Mb_substr () has a few parameters, usually 3 or 4 parameters, we especially say the last parameter, is to set the encoding

Below we look at the code by the case of different, the result of what a huge difference

$str = ' We all have a dream rventrc ';
$str = Mb_substr ($str, 0,3); Since no encoding is specified, the 1 Chinese characters are 3 characters long and output: ' I '
$str = Mb_substr ($str, 0,3, ' UTF8 '); Because the UTF8 encoding is specified, a Chinese character is 1 characters long and therefore outputs: ' We all '
$str = Mb_substr ($str, -4,2, ' UTF8 '); This means starting from the bottom 4th , capturing 2-length characters
The expression starts at subscript 2 and is truncated to the 5th character (minus 5th), so the result is: ' There's a dream RV '

$str = Mb_substr ($str, 2, -5, ' UTF8 ');

Through tonight's detailed analysis, we have a clearer understanding of the interception of Chinese strings, in fact, the setup code is easy to solve,

If you feel that the article is helpful to yourself, be free to Play Rewards a little Oh!


The above describes the PHP string splitting function mb_substr, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

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