Home > Article > Backend Development > Analysis of the differences between php functions substr(), mb_substr() and mb_strcut
This article introduces the usage and differences of the string interception function in php, substr(), mb_substr() and mb_strcut. Friends in need can refer to it.
First, let’s look at the substr function. The PHP substr() function is used to split text, but if the text to be split includes Chinese characters, there will be problems. At this time, 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 is added at the end of mb_substr()/mb_strcut to set the encoding of the string. However, most servers php_mbstring.dll is not opened. You need to open php_mbstring.dll in php.ini. Example: <?php echo mb_substr('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'); ?> Output: This way my words <?php echo mb_strcut('这样一来我的字符串就不会有乱码^_^', 0, 7, 'utf-8'); ?> Output: like this It can be seen that mb_substr splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character. MBstring function description: PHP's mbstring extension module provides multi-byte character processing capabilities. The most commonly used method is to use mbstring to split multi-byte Chinese characters. This can avoid the occurrence of half characters. Since it is an extension of PHP, its The performance is also better than some custom multi-byte segmentation functions. mbstring extension provides several functions with similar functions, mb_substr and mb_strcut. See the explanation of them in the manual. 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. Look at the following example. There is a piece of text that is segmented using mb_substr and mb_strcut: <?php $str = '我是一串比较长的中文-bbs.it-home.org'; echo "mb_substr:" . mb_substr($str, 0, 6, 'utf-8'); echo "<br>"; echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8'); ?> Output result: mb_substr: I am a string of comparisons mb_strcut:I am Carefully understand the differences between the three, so that you can apply these three functions substr, mb_substr and mb_strcut in the correct situation. |