千家信息网

php中如何将gb2312转换为utf8

发表于:2025-11-08 作者:千家信息网编辑
千家信息网最后更新 2025年11月08日,这篇文章给大家介绍php中如何将gb2312转换为utf8,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。php gb2312转换为utf8的方法:1、打开相应的PHP文件;2、检
千家信息网最后更新 2025年11月08日php中如何将gb2312转换为utf8

这篇文章给大家介绍php中如何将gb2312转换为utf8,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

php gb2312转换为utf8的方法:1、打开相应的PHP文件;2、检查原始编码;3、通过使用preg_replace和iconv函数将gb2312页面转换为utf8页面即可。

本文操作环境:Windows7系统、PHP7.1、Dell G3电脑。

php gb2312怎么转换为utf8?

PHP gb2312页面 转换为utf8页面

用到2个函数 preg_replace, iconv

  $html = preg_replace('/charset=gb2312/', 'charset=UTF8', $html);     // $enc = mb_detect_encoding($html);     $html = iconv('gbk', 'utf-8', $html);

https://php.net/manual/en/function.iconv.php

https://www.php.net/manual/en/function.preg-replace.php

参考php官网的示例

Example #2 Using indexed arrays with preg_replace()Example #1 iconv() example

* testurl.php

getMessage(), $e->getCode());}

* Curl.php

 $u,            'apikey' => '******', // 发邮件至aiqing174@qq.com索取apikey        ]);    }    public static function testURL($u) {        $api = "http://qbview.url.cn/getResourceInfo";        return self::get($api, [            'appid' => 31,            'url' => $u        ]);    }    /**     * @param $host string     * @param $params array assoc     * @return mixed     * @throws Exception     */    protected static function get($host, $params) {        $ch = curl_init();        $headers = [            "Accept: application/json",            "X-Requested-with: XMLHttpRequest",        ];        $qs = self::buildQuery($params);        $fullURL = sprintf("%s?%s", $host, $qs);        curl_setopt_array($ch, [            CURLOPT_URL => $fullURL,            CURLOPT_HEADER => 0,            CURLOPT_RETURNTRANSFER => 1,            CURLOPT_HTTPHEADER => $headers,            CURLOPT_CONNECTTIMEOUT => 5        ]);        $data = curl_exec($ch);        $errno = curl_errno($ch);        if ($errno) {            throw new Exception("%s\n", curl_error($ch), $errno);        }        curl_close($ch);        return $data;    }}

原网页内容如下:

͡ʾ        

ţ½«Ҫ·Ďʵ

https://www.sk666.vip

´畚°²ȫ·萕£¬Ϊ±£֏źµİ²ȫ£¬ӑ°?9½ء£

关于php中如何将gb2312转换为utf8就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

0