我的頁面是utf-8,file_get_contents的頁面是gb2312,輸出時中文亂碼。
解決方法如下:
<?php header("Content-Type:text/html;charset=utf-8"); $keyworld="煤層"; $keyworld=iconv("utf-8","gb2312",$keyworld); $url = "http://www.baidu.com/s?f=8&wd=$keyworld"; $html = file_get_contents($url); $html = iconv("gb2312", "utf-8//IGNORE",$html); echo $html; ?>
轉載:https://blog.csdn.net/qinglifeng/article/details/53927920
------------------------------------------------------------------------自己項目------------------------------------------------------------------




// 支持事先使用靜態方法設置Request對象和Config對象 //extend目錄 define('DS', DIRECTORY_SEPARATOR); defined('APP_PATH') or define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']) . DS); defined('ROOT_PATH') or define('ROOT_PATH', dirname(realpath(APP_PATH)) . DS); defined('VENDOR_PATH') or define('VENDOR_PATH', ROOT_PATH . 'vendor' . DS); defined('EXTEND_PATH') or define('EXTEND_PATH', ROOT_PATH . 'extend' . DS); defined('PUBLIC_PATH') or define('PUBLIC_PATH', ROOT_PATH . 'public' . DS); defined('RUNTIME_PATH') or define('RUNTIME_PATH', ROOT_PATH . 'runtime' . DS); defined('LOG_PATH') or define('LOG_PATH', RUNTIME_PATH . 'log' . DS); defined('CACHE_PATH') or define('CACHE_PATH', RUNTIME_PATH . 'cache' . DS); //緩存地址 cache //defined('TEMP_PATH') or define('TEMP_PATH', RUNTIME_PATH . 'temp' . DS);
public function save(Request $request) { header("content-type:text/html;charset=utf-8"); $file_src = $request->param('file_src'); $path = PUBLIC_PATH.$file_src; $file_text = file_get_contents($path); $content = iconv("gb2312", "utf-8//IGNORE",$file_text); dump($content);die; // file_get_content }
