Home > Article > Backend Development > Examples of php intercepting Chinese strings and getting the number of characters in Chinese strings
In the previous article, we introduced an example of intercepting characters in PHP. We only introduced the string of intercepting integer . So what about the example where we want to intercept Chinese strings? To achieve it, today I will introduce to you an example of intercepting Chinese strings and obtaining the number of characters in Chinese strings in PHP!
1. Chinese interception: mb_substr()
mb_substr( $str, $start, $length, $encoding )
$str, the string that needs to be truncated
$start , the starting point of truncation, the starting point is 0
$length, the number of words to be intercepted
$encoding, web page encoding, such as utf-8, GB2312, GBK
Example:
The code is as follows:
<?php $str='中文网:http://www.php.cn'; echo mb_substr($str,0,3,'utf-8');//截取头3个字,假定此代码所在php文件的编码为utf-8 ?>
The result shows: Chinese website
2. Get the Chinese length: mb_strlen()
mb_strlen( $str, $encoding )
$str, the string to calculate the length
$encoding, web page encoding, such as utf-8, GB2312, GBK
Example:
The code is as follows:
<?php $str='脚本之家:http://www.jb51.net'; echo mb_strlen($str,'utf-8');//假定此代码所在php文件的编码为utf-8 ?>
The result shows: 24
Summary:
Through the study of this article, we know that php intercepts strings The integer strings are completely different from the use function of Chinese strings. Friends, please don’t make a mistake!
Related recommendations:
PHP intercepts string function substr() function example usage detailed explanation
php intercepts string (no garbled utf8)
The above is the detailed content of Examples of php intercepting Chinese strings and getting the number of characters in Chinese strings. For more information, please follow other related articles on the PHP Chinese website!