Home >php教程 >php手册 >php mb_substr()函数截取中文字符串应用示例,

php mb_substr()函数截取中文字符串应用示例,

WBOY
WBOYOriginal
2016-06-13 09:28:07935browse

php mb_substr()函数截取中文字符串应用示例,

substr()函数用来截取字符串,但是对于中文字符会出现问题,而mb_substr()和mb_strcut这两个函数可以,用法与substr()相似,只是在函数最后要加入多一个参数,以设定字符串的编码,使用这两个函数需要在php.ini中把php_mbstring.dll打开。

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

输出的结果:

21
你好我好...
你...

从上面的例子可以看出,mb_substr是按字来分割字符,而mb_strcut是按字节来分割字符,但是都不会产生半个字符的现象。

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