A brief analysis of the differences and usage of PHP substr, mb_substr and mb_strcut_PHP Tutorial

WBOY
Release: 2016-07-21 15:04:27
Original
1429 people have browsed it

The PHP substr() function can split text, but you will often encounter problems if the text to be split includes Chinese characters. In this case, you can use the mb_substr()/mb_strcut function. The usage of mb_substr()/mb_strcut is similar to substr(), except that One more parameter needs to be added at the end of mb_substr()/mb_strcut to set the encoding of the string. However, most servers do not open php_mbstring.dll. You need to open php_mbstring.dll in php.ini.
For example:

Copy the code The code is as follows:

echo mb_substr('This way my string will not be garbled^_^', 0, 7, 'utf-8');
?>

Output: In this way, my words
Copy code The code is as follows:

echo mb_strcut('This way my string will not be garbled^_^', 0, 7, 'utf-8');
?>

Output: Like this
As can be seen from the above example, mb_substr splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character. ……
Explanation of mbstring function:
php’s mbstring extension module provides multi-byte character processing capabilities. The most commonly used method is to use mbstring to segment multi-byte Chinese characters. , this can avoid the situation of half a character. Since it is an extension of PHP, its performance is better than some custom multi-byte segmentation functions.
The mbstring extension provides several functions with similar functions, mb_substr and mb_strcut. See their explanation in the manual.
Copy code The code is 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. First character's position is 0. Second character position is 1, and so on.
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 first byte of multi-byte character.
It subtracts string from str that is shorter than length AND character that is not part of multi-byte string or not being middle of shift sequence.

For another example, there is a text segmented using mb_substr and mb_strcut respectively:
PLAIN TEXT
CODE:
Copy the code The code is as follows:

$str = 'I am a relatively long string of Chinese-www.webjx.com';
echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8');
echo "
";
echo "mb_strcut:" . mb_strcut($str, 0, 6 , 'utf-8');
?>

The output result is as follows:
mb_substr: I am a string of comparisons
mb_strcut :I am

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327759.htmlTechArticlePHP substr() function can split text, but if the text to be split includes Chinese characters, you will often encounter problems. At this time, you can use the function mb_substr()/mb_strcut, mb_substr() /mb_strcu...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template