|
仔細看了下php的文檔(不知道大家都是怎么寫代碼的,其實文檔上很清楚啊),上面關(guān)于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的是,這個函數(shù)不存在?!似乎php 6才支持。不過天無絕人之路,還有“用戶自定義上下文屬性”可以用。
經(jīng)過更仔細的看文檔,最后解決了這個問題:
復(fù)制代碼 代碼如下:
//設(shè)置流的編碼格式,這是文件流(file),如果是網(wǎng)絡(luò)訪問,file改成http
$opts = array('file' => array('encoding' => 'gb2312'));
$ctxt = stream_context_create($opts);
file_get_contents(文件名, FILE_TEXT, $ctxt);
php技術(shù):php 讀取文件亂碼問題,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。