On the difference and usage _php skills of PHP substr,mb_substr and Mb_strcut

Source: Internet
Author: User
PHP substr () function can split the text, but the text to be segmented if the inclusion of Chinese characters often encounter problems, then you can use the MB_SUBSTR ()/mb_strcut This function, MB_SUBSTR ()/mb_strcut use and substr () similar, Only in Mb_substr ()/mb_strcut finally to add a parameter to set the string encoding, but the general server did not open the Php_mbstring.dll, need to be in the php.ini in the Php_mbstring.dll open.
As an example:
Copy Code code as follows:

<?php
echo mb_substr (' So my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>

output: so that my word
Copy Code code as follows:

<?php
echo mb_strcut (' So my string will not have garbled ^_^ ', 0, 7, ' utf-8 ');
?>

output: Such a
From the above example can be seen, MB_SUBSTR is the word to split the characters, and Mb_strcut is the byte to split the characters, but will not produce half a character phenomenon ...
description of the mbstring function:
PHP's mbstring Extension module provides the ability to handle multibyte characters, the most commonly used is the use of mbstring to split multi-byte Chinese characters, so that you can avoid half a character situation, because it is the expansion of PHP, it's performance than some custom multi-byte segmentation function better than some.
Mbstring extension provides several functions similar to the functions, Mb_substr and Mb_strcut, to see the manual on their interpretation.
Copy Code code as follows:

Mb_substr
MB_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. The character ' s position is 0. Second character position is 1, and.
Mb_strcut
Mb_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 the multi-byte.
It subtracts string from str this is shorter than length and character this is not part of multi-byte string or not being middle of shift sequence.

For example, there is a paragraph of text, respectively, using MB_SUBSTR and mb_strcut to do the segmentation:
PLAIN TEXT
CODE:
Copy Code code as follows:

<?php
$str = ' I am a long string of Chinese-www.webjx.com ';
echo "MB_SUBSTR:". Mb_substr ($str, 0, 6, ' utf-8 ');
echo "<br>";
echo "Mb_strcut:". Mb_strcut ($str, 0, 6, ' utf-8 ');
?>

The output results are as follows:
Mb_substr: I'm a bunch of comparisons
Mb_strcut: I am

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.