激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - PHP解析html類庫simple_html_dom的轉(zhuǎn)碼bug

PHP解析html類庫simple_html_dom的轉(zhuǎn)碼bug

2020-06-30 12:07php技術(shù)網(wǎng) PHP教程

這篇文章主要介紹了PHP解析html類庫simple_html_dom的轉(zhuǎn)碼bug ,需要的朋友可以參考下

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

我這一版的simple_html_dom有一個(gè)方法 convert_text 是這個(gè)樣子的。

 

復(fù)制代碼 代碼如下:

 // 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;
 }

 

來看這一行:

 

復(fù)制代碼 代碼如下:

    $converted_text = iconv($sourceCharset, $targetCharset, $text); 

 

會(huì)引起轉(zhuǎn)碼不正確。比如會(huì)把gb2312的文字轉(zhuǎn)成:

 

復(fù)制代碼 代碼如下:

4月26日在<span style="color:#C03">鏈濋槼</span>公園馬術(shù)場舉行的2014浪琴國際馬聯(lián)場地障礙世界杯中國聯(lián)賽資格賽上,24歲的韓壯壯不僅拿到零罰分的成績 ...第7個(gè)出場的<span style="color:#C03">鍖椾含</span>奧運(yùn)騎手趙志文第一個(gè)收獲零罰分,用時(shí)77秒07 ...

 

既成的事實(shí)了,證明里頭的轉(zhuǎn)碼功能沒有處理好。由于我使用這個(gè)simple_html_dom只是想要用來構(gòu)建dom。我并沒有打算花時(shí)間去很好地處理這個(gè)bug。而是簡單地把

 

復(fù)制代碼 代碼如下:

$converted_text = iconv($sourceCharset, $targetCharset, $text);

 

改成

 

復(fù)制代碼 代碼如下:

$converted_text = $text;

 

就行了。思路就是取消它的轉(zhuǎn)碼。好吧工作不必糾結(jié),可以繼續(xù)了。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成人福利在线视频 | 久久国产免费视频 | v11av在线视频成人 | 一级毛片电影网 | 国产精品久久久久久久久久久久久久久久 | 久久人人人| av在线免费观看网 | 国产精品刺激对白麻豆99 | 亚洲精品一区二区三区免 | 免费一及片 | 久成人 | 亚洲午夜精品视频 | 久色亚洲| 欧美三级短视频 | 特大黑人videos与另类娇小 | 亚洲一区二区三区视频 | 九色国产| 毛片免费在线播放 | 成年人在线免费播放视频 | 久久久久久久久浪潮精品 | 国产九九 | 蜜桃一本色道久久综合亚洲精品冫 | 最新亚洲国产 | 成人福利在线观看 | 国产精品自拍99 | 成人乱码一区二区三区不卡视频 | 成人毛片视频免费看 | 逼片视频 | 一级毛片在线免费观看视频 | 国产一区二区精品在线观看 | 日韩av在线影院 | 国产毛片在线看 | 久久精品视频3 | 久久精品视频免费 | 91真视频| 色七七亚洲 | 日韩毛片在线看 | 手机免费看一级片 | 亚洲一二区视频 | 久久亚洲第一 | 深夜精品福利 |