A brief analysis of the differences and usages of PHP substr mb_substr and mb_strcut

  • 2020-06-15 07:58:32
  • OfStack

The PHP substr() function can split text, but it is often a problem if the text to be split includes Chinese characters. In this case, mb_substr()/mb_strcut can be used. mb_substr() /mb_strcut is similar to substr(), except that mb_substr()/mb_strcut adds one more argument to set the encoding of the string. However, es15EN_mbstring.dll is not open like the server 1. You need to open php_mbstring.dll in php.ini.
Here's an example:

<?php
echo mb_substr(' such 1 I don't have to mess up my string ^_^', 0, 7, 'utf-8');
?>

Output: This 1 comes to my word

<?php
echo mb_strcut(' such 1 I don't have to mess up my string ^_^', 0, 7, 'utf-8');
?>

Output: so 1
As you can see from the above example, mb_substr is used to slice characters by word, while mb_strcut is used to slice characters by byte, but it doesn't produce half a character...
Description of mbstring function:
The mbstring extension module of php provides multi-byte character processing capability. The most commonly used is mbstring to shard multi-byte Chinese characters, so that half a character can be avoided. Due to the extension of php, it performs better than some custom multi-byte shard functions.
mbstring extension provides several similar functions, mb_substr and mb_strcut, see the manual for an explanation of them.

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.

mb_substr and mb_strcut
PLAIN TEXT
CODE:

<?php
$str = ' I am a 1 The string is longer in 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 1 string comparison
mb_strcut: I am

Related articles: