四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

PHP解析html類庫simple_html_dom的轉碼bug

這幾天有在用simple_html_dom抓一些文章。不同網站的編碼在國內基本上是gbk gb2312 utf-8。而以gb2312和utf-8居多。

我這一版的simple_html_dom有一個方法 convert_text 是這個樣子的。

復制代碼 代碼如下:
 // PaperG - Function to convert the text from one character set to another if the two sets are not the same.
 function convert_text($text)
 {
  global $debug_object;
  if (is_object($debug_object)) {$debug_object->debug_log_entry(1);}
  $converted_text = $text;
  $sourceCharset = "";
  $targetCharset = "";
  if ($this->dom)
  {
   $sourceCharset = strtoupper($this->dom->_charset);
   $targetCharset = strtoupper($this->dom->_target_charset);
  }
  if (is_object($debug_object)) {$debug_object->debug_log(3, "source charset: " . $sourceCharset . " target charaset: " . $targetCharset);}
  if (!empty($sourceCharset) && !empty($targetCharset) && (strcasecmp($sourceCharset, $targetCharset) != 0))
  {
   // Check if the reported encoding could have been incorrect and the text is actually already UTF-8
   if ((strcasecmp($targetCharset, 'UTF-8') == 0) && ($this->is_utf8($text)))
   {
    $converted_text = $text;
   }
   else
   {
    $converted_text = iconv($sourceCharset, $targetCharset, $text);
   }
  }
  // Lets make sure that we don't have that silly BOM issue with any of the utf-8 text we output.
  if ($targetCharset == 'UTF-8')
  {
   if (substr($converted_text, 0, 3) == "/xef/xbb/xbf")
   {
    $converted_text = substr($converted_text, 3);
   }
   if (substr($converted_text, -3) == "/xef/xbb/xbf")
   {
    $converted_text = substr($converted_text, 0, -3);
   }
  }
  return $converted_text;
 }

來看這一行:

復制代碼 代碼如下:
    $converted_text = iconv($sourceCharset, $targetCharset, $text); 

會引起轉碼不正確。比如會把gb2312的文字轉成:

復制代碼 代碼如下:
4月26日在<span style="color:#C03">

主站蜘蛛池模板: 《万万没想到》电影| 心心相印抽纸| 细菌大战2| 虞书欣新剧永夜星河免费观看| 菲律宾电影毕业生代表| 迷夜电影| 我姨| 电影英雄| 北之萤| 烟花女驼龙| 桥梁工程施工方案| 嗯啊不要啊啊啊| 公司减资从1000万减到10万| 3d怎么玩| 保镖1993电视剧正版免费观看| 创业史全文免费阅读| 色戒在线观看视频| 扎西顿珠的个人资料简介 | 天气预报电影| 安志杰电影全集大全| 新上映电影| 流浪地球2视频免费播放下载| 二年级53天天练语文上册答案| 意外的春天 电影| 电影《uhaw》完整版| 正在行动| 薄冰演员表| 单招在线咨询| 第一财经直播电视直播| 想太多吉他谱| 内蒙古通辽地图| 中女| 荒笛子简谱| 抖音手机网页版入口| 帕罗| 二年级上册期末真题卷| 张寿懿| 刀客家族的女人演员表| 《致命弯道3》| 难兄难弟 电视剧| r1se成员|