PHP解析html类库simple_html_dom的转码bug

2015-01-24信息快讯网

这篇文章主要介绍了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"></span>公园马术场举行的2014浪琴国际马联场地障碍世界杯中国联赛资格赛上,24岁的韩壮壮不仅拿到零罚分的成绩 ...第7个出场的<span style="color:#C03">浜</span>奥运骑手赵志文第一个收获零罚分,用时77秒07 ...

既成的事实了,证明里头的转码功能没有处理好。由于我使用这个simple_html_dom只是想要用来构建dom。我并没有打算花时间去很好地处理这个bug。而是简单地把

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

改成

$converted_text = $text; 

就行了。思路就是取消它的转码。好吧工作不必纠结,可以继续了。

Windows下的PHP安装pear教程
Windows下安装PHP单元测试环境PHPUnit图文教程
PDO防注入原理分析以及使用PDO的注意事项总结
PHP启动windows应用程序、执行bat批处理、执行cmd命令的方法(exec、system函数详解)
php中__destruct与register_shutdown_function执行的先后顺序问题
php 模拟 asp.net webFrom 按钮提交事件实例
Chrome Web App开发小结
ThinkPHP调用common/common.php函数提示错误function undefined的解决方法
修改destoon会员公司的伪静态中的com目录的方法
从零开始学YII2框架(二)通过 Composer 安装扩展插件
Codeigniter框架的更新事务(transaction)BUG及解决方法
async和DOM Script文件加载比较
PHP使用DOMDocument类生成HTML实例(包含常见标签元素)
用PHP代替JS玩转DOM的思路及示例代码
PHP is_subclass_of函数的一个BUG和解决方法
PHP+Mysql+Ajax+JS实现省市区三级联动
浅谈discuz密码加密的方式
PHP函数strip_tags的一个bug浅析
PhpDocumentor 2安装以及生成API文档的方法
Windows中使用计划任务自动执行PHP程序实例
Windows和Linux中php代码调试工具Xdebug的安装与配置详解
PHP批量检测并去除文件BOM头代码实例
windows服务器中检测PHP SSL是否开启以及开启SSL的方法
Windows下的PHP安装文件线程安全和非线程安全的区别
PHP批量删除、清除UTF-8文件BOM头的代码实例
php警告Creating default object from empty value 问题的解决方法
PHP调用VC编写的COM组件实例
使用php清除bom示例
PHP添加Xdebug扩展的方法
用Zend Studio+PHPnow+Zend Debugger搭建PHP服务器调试环境步骤
php安装xdebug/php安装pear/phpunit详解步骤(图)
DOM XPATH获取img src值的query
php解析html类库simple_html_dom(详细介绍)
浅析php插件 Simple HTML DOM 用DOM方式处理HTML
基于simple_html_dom的使用小结
php setcookie(name, value, expires, path, domain, secure) 参数详解
©2014-2024 dbsqp.com