一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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">

主站蜘蛛池模板: 久久久久久一级毛片免费无遮挡 | 亚洲性小说| 皮皮在线精品亚洲 | www.五月婷婷.com | 亚州毛色毛片免费观看 | 狠狠干五月天 | 亚洲视频在线观看网站 | xxxxx免费| 午夜视频在线观看一区 | 91精品视频播放 | 六月丁香久久 | 久久激情小说 | 高清一区二区在线观看 | 欧美人体大胆艺术 | 国产成人精品一区二区仙踪林 | 欧亚激情偷乱人伦小说视频 | 加勒比精品 | 91久久精品一区二区 | 国内精品视频区在线2021 | 久久国产精品国语对白 | 伊人五月婷 | 日韩每日更新 | 国产亚洲精品sese在线播放 | 成人福利在线视频 | 性做久久久久久久久男女 | 国产欧美一区二区三区在线看 | 女人l8毛片a一级毛片 | selaoban在线视频免费精品 | 国产资源视频 | 黑人www| 成 人免费va视频 | 国产成人综合一区精品 | 色婷婷五| 韩国美女一级毛片 | 蜜桃视频成人免费版在线 | 韩国毛片| 草草在线免费视频 | 欧美三级视频在线观看 | www.91麻豆| 亚洲国产成人在线视频 | 在线观看成人小视频 |