PHP mb_substr (), mb_strcut () functions that intercept Chinese string lengths

Source: Internet
Author: User
SUBSTR () function can be used to split the text, but the text to be divided if the inclusion of Chinese characters tend to encounter problems, you can use MB_SUBSTR ()/mb_strcut This function, Mb_substr ()/mb_strcut usage and substr () similar, Just in Mb_substr ()/mb_strcut to add more than one parameter to set the string encoding, but the general server did not open Php_mbstring.dll, need to php.ini in the Php_mbstring.dll to open.

As an example:

<?phpecho mb_substr (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8′); >

Output: so that my word

<?phpecho mb_strcut (' This way my string will not have garbled ^_^ ', 0, 7, ' utf-8′); >

Output: this

As can be seen from the above example, MB_SUBSTR is the word to divide the characters, and mb_strcut is to divide the characters by Byte, but will not produce a half-character phenomenon ...

The above paragraph is a excerpt from the network, the results are tested by myself.

Personal Understanding:
The Mb_substr () function represents one unit for English or Chinese characters.
The Mb_strcut () function pairs Chinese characters in 3 units, and English is 1 units.

For example:

<?php$str = "Such ABCD Comes", echo "Mb_substr:". Mb_substr ($str, 0, 5, ' utf-8′); echo "<br>", echo "Mb_strcut:". mb_ Strcut ($str, 0, 8, ' utf-8′);? >

The output results are as follows:

MB_SUBSTR: So abcmb_strcut:  so AB

Report:

The difference between strlen and Mb_strlen:

<?php $str = "Chinese a character 1 characters"; echo strlen ($STR); echo "<br/>"; Echo Mb_strlen ($str, ' utf8′);?>

Output Result:

14 6

The above is PHP intercept Chinese string length mb_substr (), mb_strcut () 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.