file_get_contents 输出乱码问题

我的页面是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
    }

 

posted @ 2020-09-26 16:15  搬砖小伙子  阅读(2069)  评论(0编辑  收藏  举报