Home  >  Article  >  Backend Development  >  Application example of php mb_substr() function to intercept Chinese string_PHP tutorial

Application example of php mb_substr() function to intercept Chinese string_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:21:45680browse

php mb_substr() function interception Chinese string application example

The substr() function is used to intercept strings, but there will be problems with Chinese characters, and mb_substr() and mb_strcut These two functions can be used. Here is an introduction to their specific usage

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.

Header("content-type:text/html; charset=utf-8");

$string = "Hello, hello, hello everyone";

echo strlen($string).'
';

echo mb_substr($string,0,4,'utf-8').'...
';

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 splits characters by words, while mb_strcut splits characters by bytes, but neither will produce half a character.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/852829.htmlTechArticlephp mb_substr() function intercepts Chinese string application example substr() function is used to intercept strings, but for Chinese There will be problems with characters, and the two functions mb_substr() and mb_strcut can...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn