Application example of php mb_substr() function to intercept Chinese strings

PHP中文网
Release: 2016-07-20 11:06:26
Original
835 people have browsed it

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. Here is an introduction to their specific usage

substr() function 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 must be added at the end of the function to set the encoding of the string. , to use these two functions, you need to open php_mbstring.dll in php.ini.

<?php 
header("content-type:text/html; charset=utf-8");
$string = "你好我好大家好";
echo strlen($string).&#39;</br>&#39;;
echo mb_substr($string,0,4,&#39;utf-8&#39;).&#39;...</br>&#39;;
echo mb_strcut($string,0,4,&#39;utf-8&#39;).&#39;...&#39;;
?>
Copy after login

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.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template