Home > Article > Backend Development > Application example of php mb_substr() function to intercept Chinese strings
The substr() function is used to intercept strings, but there will be problems with Chinese characters. The two functions mb_substr() and mb_strcut can be used. The usage is similar to substr(), except that one more parameter needs to be added at the end of the function. To set the encoding of the string, using these two functions requires opening php_mbstring.dll in php.ini.
<?php header("content-type:text/html; charset=utf-8"); $string = "你好我好大家好"; echo strlen($string).'</br>'; echo mb_substr($string,0,4,'utf-8').'...</br>'; echo mb_strcut($string,0,4,'utf-8').'...'; ?>
Output result:
21
Hello, I’m good...
You...
As can be seen from the above example, mb_substr It splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character.
For more articles related to the application examples of the php mb_substr() function to intercept Chinese strings, please pay attention to the PHP Chinese website!