In PHP, you can use the mb_substr() function to intercept a Chinese string. This function can intercept a specified part of a string. It is not only valid for English characters, but also for Chinese characters; the syntax format is " mb_substr($str,$start,$length,$encoding)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
mb_substr() function can select from a string Intercept the specified part. Different from the substr() function, the mb_substr() function is not only valid for English characters, but also for Chinese characters. Its syntax format is as follows:
mb_substr($str , $start [, $length = NULL [, $encoding = mb_internal_encoding()]])
The parameter description is as follows:
[Example] Use the mb_substr() function to intercept a Chinese string of specified length.
'; echo mb_substr($str, -19).'
'; echo mb_substr($str, 4, 6).'
'; echo mb_substr($str, 4, -16).'
'; echo mb_substr($str, -19, -13).'
'; echo mb_substr($str, -19, 6).'
'; var_dump(mb_substr($str, 40)); echo '
'; var_dump(mb_substr($str, 4, null)); ?>
The running results are as follows:
PHP中文网,一个在线学习编程的网站。 PHP中文网,一个在线学习编程的网站。 PHP中文网 PHP PHP中文网 PHP中文网 string(0) "" string(55) "PHP中文网,一个在线学习编程的网站。"
[Example] Use the mb_substr() function to intercept a string of specified length, and replace the excess part with "...".
18){ echo mb_substr($str, 0, 18).'...'; }else{ echo $str; } ?>
The running results are as follows:
PHP中文网是一个在线学习编程的网站...
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement Chinese string interception in php. For more information, please follow other related articles on the PHP Chinese website!