将一个短信接口代码从apache迁移到nginx+php-fpm后,发现无法发出短信了,查看php日志, [25-sep-2014 20:15:21] warning: [pool www] child 9617 said into stderr: notice: php message: php fatal error: ?call to undefined function mb_convert_encodin
将一个短信接口代码从apache迁移到nginx+php-fpm后,发现无法发出短信了,查看php日志,
[25-sep-2014 20:15:21] warning: [pool www] child 9617 said into stderr: “notice: php message: php fatal error: ?call to undefined function mb_convert_encoding() in /data/htdocs/xx.php on line 13″
发现函数mb_convert_encoding没定义,看着像某个模块没装,google了把,要装个mbstring扩展,之前都是一下装好多扩展(虽然不知道这个扩展是干啥的,按照网络文档来),现在是要应用需要哪个装哪个,逼格略有提高(至少这样能让我知道哪个模块是干啥的)。
mb-convert-encodingstring mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding = mb_internal_encoding() ] )
将 string 类型 str 的字符编码从可选的 from_encoding 转换到 to_encoding。
官网文档 ?http://php.net/manual/zh/function.mb-convert-encoding.php 需要安装mbstring扩展库,如果已经编译好的php可以这样热编译下
cd /tmp/php-5.3.28/ext/mbstring/usr/local/services/php/bin/phpize./configure --with-php-config=/usr/local/services/php/bin/php-configmake && make installvim /usr/local/services/php/etc/php.iniextension=/usr/local/services/php/lib/php/extensions/no-debug-non-zts-20090626/mbstring.so;
?iconvstring iconv ( string in_charset, string out_charset, string str )
iconv函数库能够完成各种字符集间的转换
注意:第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://translit 和 //ignore,其中 //translit 会自动将不能直接转化的字符变成一个或多个近似的字符,//ignore 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
returns the converted string or false on failu
官网地址 ?http://php.net/manual/zh/book.iconv.php
已经安装好php的,同样也可以使用上面的方法安装iconv模块
mb_convert_encoding例子mb_convert_encoding这个函数是用来转换编码的。英文一般不会存在编码问题,只有中文数据才会有这个问题。比如你用zend studio或editplus写程序时,用的是gbk编码,如果数据需要入数据库,而数据库的编码为utf8时,这时就要把数据进行编码转换,不然进到数据库就会变成乱码
做一个gbk to utf-8
再来个gb2312 to big5
mb_strtolower() – 使字符串小写mb_strtoupper() – 使字符串大写strtolower() – 将字符串转化为小写strtoupper() – 将字符串转化为大写ucfirst() – 将字符串的首字母转换为大写ucwords() – 将字符串中每个单词的首字母转换为大写?iconv例子把gb2312置换成utf-8:
1 $text=iconv(gb2312,utf-8,$text);
在用$text=iconv(“utf-8″,”gb2312″,$text)过程中,如果遇到一些特别字符时,如:”—”,英文名中的”.”等等字符,转换就断掉了。这些字符后的文字都没法继续转换了。
针对这的问题,可以用如下代码实现:
1 $text=iconv(utf-8,gbk,$text);
你没有看错,就这么简单,不使用gb2312,而写成gbk,就可以了。
还有一种方法,第二个参数,加上//ignore,忽略错误,如下:
1 iconv(utf-8,gb2312//ignore,$data);
一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数。
$content = iconv(gbk, utf-8″, $content);$content = mb_convert_encoding($content, utf-8″, gbk);
原文地址:php下编码转换函数mb_convert_encoding与iconv, 感谢原作者分享。