Home > Article > Backend Development > Solution to PHP reading garbled files_PHP Tutorial
The default encoding of the stream reading function of PHP 5 seems to be UTF-8. In the past, it was normal to directly read the gb2312 encoding through file_get_contents() in PHP 4, but when it reached 5, it became garbled. The solution online says to use iconv() to transcode after crawling. After reading it, I felt something was wrong: one is that the iconv library may not have been compiled, and the bigger problem is that the encoding is related to the stream conversion (if iconv is used, PHP actually converts the code twice: stream-> UTF-8 -> GB2312): Isn’t this busy work in vain?
Read the php documentation carefully (I don’t know how everyone writes code, but the documentation is very clear). It is mentioned above about fopen() and file_get_contents() that "the default is UTF-8 , but the user can specify a different encoding by creating a custom context or by changing the default using stream_default_encoding().). So I used stream_default_encoding('gb2312'); to test: But faintly, this function does not exist? ! It seems that php 6 only supports it. However, there is always a way out, and there are also "user-defined context attributes" that can be used.
After reading the document more carefully, I finally solved this problem:
//Set the encoding format of the stream. This is the file stream (file). If it is network access, change file to http.
$opts = array('file' => array('encoding' => 'gb2312'));
$ctxt = stream_context_create($opts);
file_get_contents(filename, FILE_TEXT, $ctxt);