您好,欢迎访问一九零五行业门户网

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()<?php$string = 'the quick brown fox jumps over the lazy dog.';$patterns = array();$patterns[0] = '/quick/';$patterns[1] = '/brown/';$patterns[2] = '/fox/';$replacements = array();$replacements[2] = 'bear';$replacements[1] = 'black';$replacements[0] = 'slow';echo preg_replace($patterns, $replacements, $string);?>example #1 iconv() example<?php$text = "this is the euro symbol '€'.";echo 'original : ', $text, php_eol;echo 'translit : ', iconv("utf-8", "iso-8859-1//translit", $text), php_eol;echo 'ignore : ', iconv("utf-8", "iso-8859-1//ignore", $text), php_eol;echo 'plain : ', iconv("utf-8", "iso-8859-1", $text), php_eol;?>
* testurl.php
<?php/** * created by phpstorm. * user: mingzhanghui * date: 8/30/2019 * time: 09:38 */function __autoload($classname) { include dirname(__file__).'/lib/'.$classname.'.php';}header('content-type: text/html; charset=utf8');// http://47.93.27.106:8064/testurl.php?url=https://www.sk666.vip$url = $_get['url'];try { $html = curl::testurl($url); $html = preg_replace('/charset=gb2312/', 'charset=utf8', $html); // $enc = mb_detect_encoding($html); $html = iconv('gbk', 'utf-8', $html); echo $html;} catch (exception $e) { response::fail($e->getmessage(), $e->getcode());}
* curl.php
<?php/** * created by phpstorm. * user: mingzhanghui * date: 8/29/2019 * time: 15:34 */class curl{ /** * @param $map * @return string */ public static function buildquery(/* array */ $map) { $a = []; array_walk($map, function($value, $name) use (&$a) { array_push($a, sprintf("%s=%s", urlencode($name), urlencode($value))); }); return implode('&', $a); } /** * https://www.ft12.com * @param $u * @return mixed * @throws exception */ public static function shorturl($u) { $api = "http://api.ft12.com/api.php"; return self::get($api, [ 'url' => $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; }}
原网页内容如下:
<html><head><meta http-equiv="content-language" content="zh-cn"><meta http-equiv="content-type" content="text/html; charset=gb2312"><meta id="viewport" name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"><meta content="telephone=no" name="format-detection"><link rel="stylesheet" type="text/css" href="http://rd.wechat.com/static/zh_cn/default/css/redirect_confirm.css?v=1375841702"><title>͡ʾ</title><style>.container { }.sectioncontent { text-shadow:0 1px 0 #f5f5f5;font-size:18px; }.sectionpannel {}.firstp .lastp{ text-shadow:0 1px 0 #f5f5f5;}.lastp { font-size:14px; }a.normallink { font-size:14px;text-decoration:none;color:#1587c1;font-weight:bold;text-shadow:0 1px 0 #f5f5f5; }.confirm{padding:20px 15px;}.confirm a.icon_info { display:block;border-radius:20px;background:#999;font-weight:bold;font-size:14px;color:#eee;text-shadow:0 1px 0 #333;border-top:1px solid #ddd;border-bottom:1px solid #fff;width:20px;height:20px;line-height:20px;margin:0 auto;text-decoration:none;padding:0;font-family:georgia; }.confirm .greenbtn b{font-weight:400}.minorlink {margin-top: 20px;padding:10px;font-weight:normal;font-size:14px;text-shadow:0 1px 0 #f5f5f5;text-decoration:none;color:#737373;line-height:1.5;background:transparent url("http://rd.wechat.com/static/zh_cn/default/img/bg_line.png?v=") no-repeat top center;}.sectionpannel .sectioncontent .url{max-height:100%;}.url_box{ margin: 5px; padding: 5px 0; border-top: 1px solid #b5b5b5; border-bottom: 1px solid #b5b5b5; box-shadow: inset 0 1px 0px #fefefe, 0 1px 0px #fefefe;}.url_box .url_wrp{margin: 0; max-height: 72px; overflow: hidden;}.icon_warn{height:160px;background:url("http://rd.wechat.com/static/zh_cn/default/img/icon_warn.png?v=") no-repeat center;margin-bottom:20px;-webkit-background-size:160px;background-size:160px;}.icon_link{height:100px;background:url("http://rd.wechat.com/static/zh_cn/default/img/icon_link.png?v=") no-repeat center;margin-bottom:20px;-webkit-background-size:100px;background-size:100px;}@media all and (-webkit-min-device-pixel-ratio: 1.5) {.icon_warn{background-image:url("http://rd.wechat.com/static/zh_cn/default/img/icon_warn_1.5x.png?v=");-webkit-background-size:160px;background-size:160px;}.icon_link{background-image:url("http://rd.wechat.com/static/zh_cn/default/img/icon_link_1.5x.png?v=");-webkit-background-size:160px;background-size:160px;}.minorlink{background-image:url("http://rd.wechat.com/static/zh_cn/default/img/bg_line_1.5x.png?v=");-webkit-background-size:320px 6px;background-size:320px 6px;}}</style></head><body> <!-- ԑɽ¸? 1 %mtt_original_url£¬֢ˇһ¸?§·ďʵŕ­ʼurl 2 %mtt_qbview_url£¬ѩҫ³ꈡ+תëµõrl£¬բļט¶¨в+³ꈡ±늶 3 %mtt_proxy_url£¬ ǖͨoאתµő鈳¡£ --><section class="container"><p class="confirm" style="padding-top:0"><!--alerticon1 use for state1,alerticon2 use for state2--><p class="sectionicon_link"></p><p class="section sectionpannel"><p class="sectioncontent" style="margin-bottom:20px;word-break: break-all"><p class="url firstp" style="color:#737373;font-size:14px;">ţ½«ҫ·ďʵ<p class="url_box"><p class="url_wrp" id="url">https://www.sk666.vip</p></p><p style="color:red">´畚°²ȫ·萕£¬ϊ±£֏źµi²ȫ£¬ӑ°?9½ء£</p></p></p></p></p></section>
推荐学习:《php视频教程》
以上就是php gb2312怎么转换为utf8的详细内容。
其它类似信息

推荐信息