In php, the mb_substr() function is a function used to intercept Chinese and English. It can quickly and easily solve the problem of intercepting the specified character length. Let me introduce it to you below.
Tip: mb_substr is not supported by default in php. We need to find php.ini in the windows directory, open it for editing, search for mbstring.dll, find ;extension=php_mbstring.dll and remove the previous; sign before it can be used. mb_substr function.
Function: mb_substr( $str, $start, $length, $encoding )
$str, the string that needs to be truncated
$start, truncation start point
$length, length (note, this is different from mb_strimwidth, 1 represents a Chinese character)
$encoding, encoding, I set it to utf-8
Example: Truncate the article title to 15 characters
The code is as follows | Copy code | ||||
$str = 'This way my string will not be garbled^_^'; echo "mb_substr:" . mb_substr($str, 0, 7, 'utf-8'); //Result: In this way, my words echo " |