Home > Article > Backend Development > PHP intercepts the mb_substr() and mb_strcut() functions of Chinese string length
The substr() function can be used to split text, but if the text to be split includes Chinese characters, you will often encounter problems. In this case, you can use the mb_substr()/mb_strcut function. The usage of mb_substr()/mb_strcut is the same as that of substr( ) is similar, 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:
<?php echo mb_substr(‘这样一来我的字符串就不会有乱码^_^’, 0, 7, ‘utf-8′); ?>
Output: This way my words
<?php echo mb_strcut(‘这样一来我的字符串就不会有乱码^_^’, 0, 7,’utf-8′); ?>
Output: This way
From above As can be seen from the example, mb_substr splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character...
The above paragraph is excerpted from the Internet , the results are all obtained through my own testing.
Personal understanding:
mb_substr() function represents a unit for English or Chinese characters.
mb_strcut() function is 3 units for Chinese characters and 1 unit for English characters.
For example:
<?php $str = “这样abcd一来”; echo “mb_substr:”.mb_substr($str, 0, 5, ‘utf-8′); echo “<br>”; echo “mb_strcut:”.mb_strcut($str, 0, 8, ‘utf-8′); ?>
The output result is as follows:
mb_substr: 这样abc mb_strcut: 这样ab
Attachment:
The difference between strlen and mb_strlen:
<?php $str=”中文a字1符”; echo strlen($str); echo “<br />”; echo mb_strlen($str,’UTF8′); ?>
Output result:
14 6
The above is the content of mb_substr() and mb_strcut() functions that PHP uses to intercept the length of Chinese strings. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!