php转码 iconv和mb_convert_encoding

最近在给公司做一个小工具,将excel表格按照一定的格式转为txt文本格式,要求转后的txt文本是GBK编码,但是总会有几个excel表格无法正常转码,最后查阅相关资料,得到解决方案

先说明下,在php里提供了两种方案可以实现转码

1.使用iconv函数,用法

string iconv ( string in_charset, string out_charset, string str )

第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,其中 //TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,//IGNORE 会忽略掉不能转化的字符,如果将某字符串从UTF-8编码转为GBK,而这个字符串中混有不是UTF-8编码的字符时,就会报错,建议加上//IGNORE

2.使用mb_convert_encoding,用法

string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )

需要开启mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉

原文地址:https://www.cnblogs.com/adobe-lin/p/10212304.html