PHP solution to reading garbled files _ PHP Tutorial

Source: Internet
Author: User
PHP solves the problem of reading garbled files. The stream reading function of php5 seems to be the default encoding is UTF-8, in the past in php4 direct file_get_contents () read the normal gb2312 encoding, to 5 it will be garbled. The solution on the Internet said to capture with php 5 Stream reading function seems to be the default encoding is UTF-8, previously in php 4 directly file_get_contents () read the normal gb2312 encoding, when it reaches 5, it will be garbled. The solution on the internet is to use iconv () for transcoding after capturing. After reading this, I realized something was wrong: one is that the iconv library is not necessarily compiled, the bigger problem is that encoding is related to stream conversion (if iconv is used, php actually turns the code twice: stream-> UTF-8-> GB2312 ): isn't that too busy?

I carefully read the php documentation (I don't know how everyone writes code, but the documentation is very clear). the above section describes fopen () and file_get_contents () all mentioned "the default is UTF-8, but you can change the encoding with stream_default_encoding () or custom context attributes" (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 ().). Use stream_default_encoding ('gb2312'); test: but the faint is, does this function not exist ?! It seems that php 6 is supported. However, there is no path to perfection, and "custom context attributes" can be used.

After reading the document more carefully, we finally solved the problem:

// Set the stream encoding format. this is a file. for network access, change file to http.
$ Opts = array ('file' => array ('encoding' => 'gb2312 '));
$ Ctxt = stream_context_create ($ opts );
File_get_contents (file name, FILE_TEXT, $ ctxt );

The stream reading function of http://www.bkjia.com/PHPjc/364477.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364477.htmlTechArticlephp 5 seems to be the default encoding is UTF-8, previously in php 4 directly file_get_contents () read the normal gb2312 encoding, to 5 it is garbled. The solution on the internet is to use it after crawling...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.