PHP 读取文件乱码的解决方法

php 5的流读取函数好像默认编码是UTF-8,以前在php 4里直接file_get_contents()读取gb2312编码的正常,到了5就乱码了。网上的解决办法说抓取后用iconv()转码。看后我就觉 得不对劲:一个是不一定编译了iconv库,更大的问题是编码都跟流转换的时候有关(如果用了iconv实际上php转了两次码:流 -> UTF-8 -> GB2312):这不是白忙乎了吗?

仔细看了下php的文档(不知道大家都是怎么写代码的,其实文档上很清楚啊),上面关于fopen()及file_get_contents()都 提到了“默认是UTF-8,但是用户可以用stream_default_encoding()或者用户自定义上下文属性改变编码”(If unicode semantics are enabled, the default encoding of the read data is UTF-8. You can specify a different encoding by creating a custom context or by changing the default using stream_default_encoding().)。于是用stream_default_encoding(’gb2312′);测试:但是 faint的是,这个函数不存在?!似乎php 6才支持。不过天无绝人之路,还有“用户自定义上下文属性”可以用。

经过更仔细的看文档,最后解决了这个问题:

//设置流的编码格式,这是文件流(file),如果是网络访问,file改成http
$opts = array(‘file’ => array(‘encoding’ => ‘gb2312’));
$ctxt = stream_context_create($opts);
file_get_contents(文件名, FILE_TEXT, $ctxt);

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » PHP 读取文件乱码的解决方法

84 评论

  1. oral zyvox 600mg gambling game ocean casino online

  2. buy methotrexate 2.5mg reglan pills purchase reglan without prescription

  3. tiotropium bromide 9 mcg price spiriva 9 mcg sale purchase hytrin generic

  4. buy nifedipine 10mg pill adalat 30mg brand cheap fexofenadine 180mg

  5. altace 5mg ca arcoxia usa oral etoricoxib 60mg

  6. buy avodart 0.5mg for sale purchase avodart order orlistat 120mg online

  7. buy escitalopram without a prescription order lexapro pill buy revia 50mg generic

  8. buy rosuvastatin generic oral zetia order domperidone sale

  9. order tamsulosin 0.2mg aldactone price purchase aldactone pills

  10. order zocor online cheap viagra canada viagra for sale

  11. cleocin 150mg generic fildena sale sildenafil buy online

  12. cheap sildenafil suhagra cost order sildalis sale

  13. order minomycin without prescription terazosin brand order actos 30mg pill

  14. order temovate generic brand buspar order cordarone online

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡