虎泰克博客

PHP截取汉字乱码问题解决方法mb_substr函数的应用


利用mb_substr截取字符串不会出现乱码问题,高手可以飞过...... dy

利用mb_substr截取字符串不会出现乱码问题,高手可以飞过......
首先
1.确保你的Windows/system32下有php_mbstring.dll这个文件,没有就从你Php安装目录extensions里拷入Windows/system32里面。

2.在windows目录下找到php.ini打开编辑,搜索mbstring.dll,找到
;extension=php_mbstring.dll把前面的;号去掉,这样mb_substr函数就可以生效了
mb_strcut函数功能也可以截取字符串长度,下面实例具体看看区别在哪:

复制代码 代码如下:

<?php
$str = '这样一来我的字符串就不会有乱码^_^';

echo "mb_substr:" . mb_substr($str, 0, 7, 'utf-8');
//结果:这样一来我的字
echo "<br>";

echo "mb_strcut:" . mb_strcut($str, 0, 6, 'utf-8');
//结果:这样
?>


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


Statement: all articles / drawings reproduced on this website are for the purpose of transmitting more information and facilitating discussion, which does not mean that the website, the host and the organizer agree with their opinions or confirm the authenticity of their contents. The contents of the articles are for reference only. If the rights of the original author are violated, please contact the website administrator.

Sign in to comment

Comment list

    No data