PHP DOMDocument-> getElementByID添加Â代替空(PHP DOMDocument->getElementByID adding  in place of empty )

我正在使用PHP的DOMDocument对象来解析一些HTML(使用cURL获取)。 当我按ID获取元素并输出它时,任何空的 标记都会获得一个额外的字符并变为Â 。

代码:

$document = new DOMDocument();

$document->validateOnParse = true;

$document->loadHTML( curl_exec($handle) );

curl_close($handle);

$element = $document->getElementById( __ELEMENT_ID__ );

echo $document->saveHTML();

echo $document->saveHTML($element);

?>

$document->saveHTML()命令按预期运行并打印出整个页面。 但是,就像我上面说的那样,在echo $document->saveHTML($element)命令echo $document->saveHTML($element)空标签转换为Â 。

这发生在$element所有 标记中。

在这个过程中(通过ID获取元素并输出元素)是插入这个额外的字符? 我可以解决它,但我更感兴趣的是找到根。

I'm using PHP's DOMDocument object to parse some HTML (fetched with cURL). When I get an element by ID and output it, any empty tags get an additional character and become  .

The Code:

$document = new DOMDocument();

$document->validateOnParse = true;

$document->loadHTML( curl_exec($handle) );

curl_close($handle);

$element = $document->getElementById( __ELEMENT_ID__ );

echo $document->saveHTML();

echo $document->saveHTML($element);

?>

The $document->saveHTML() command behaves as expected and prints out the entire page. BUT, like I say above, on the echo $document->saveHTML($element) command transforms empty tags into  .

This happens to all tags within $element.

What in this process (of getting the element by ID and outputting the element) is inserting this extra character? I'm could work around it, but I'm more interested in getting to the root.

原文:https://stackoverflow.com/questions/13629351

更新时间:2019-11-29 11:57

最满意答案

我能够通过设置页面的字符编码来解决问题。 我提取的页面没有定义的字符编码,我的页面只是一个没有定义标题信息的片段。 当我添加

问题消失了。

I was able to fix the problem by setting the character encoding of the page. The page I was fetching did not have a defined character encoding, and my page was just a snippet without defined header info. When I added

The problem disappeared.

2012-11-30

相关问答

我能够通过设置页面的字符编码来解决问题。 我提取的页面没有定义的字符编码,我的页面只是一个没有定义标题信息的片段。 当我添加

问题消失了。 I was able to fix the problem by setting the character encoding of the page. The page I was

...

你可以使用DOMDocumentFragment和它的appendXML()方法,例如 <?php

$doc = new DOMDocument();

$doc->formatOutput = true;

$ele = $doc->createElement("someele", "Hello");

$xmlstuff = $doc->createElement("otherxmlstuff");

$fragment = $doc->createDocumentFragm

...

function getInnerHtml( $node ) {

$innerHTML= '';

$children = $node->childNodes;

foreach ($children as $child) {

$innerHTML .= $child->ownerDocument->saveXML( $child );

}

return $innerHTML;

}

$html = getInnerHtml($d

...

替换&nbsp; 与&amp; nbsp; 然后当读取htmlDom文档时,它将返回&nbsp; replace   with &nbsp; then when the htmlDom doc is read it will return

您可以使用抑制解析错误的输出 libxml_use_internal_errors(true);

要检查返回的响应是否为404,您可以在调用DOMDocument::load()之后检查$http_response_header 例: libxml_use_internal_errors(true);

$rssDom = new DOMDocument();

$rssDom->load($url);

if (strpos($http_response_header[0], '404')) {

...

我认为,如果禁用外部实体加载器,则显然无法加载外部实体 。 唯一的解决方案是使用libxml_disable_entity_loader(false)启用外部实体的加载。 由于此设置不是线程安全的,我可以看到两种方法: 全局启用它并使用其他功能来阻止加载不需要的实体(通常来自网络): 使用libxml_set_external_entity_loader注册您自己的实体加载器。 我认为这是最安全的解决方案。 使用解析选项LIBXML_NONET 。 如果您只想禁用libxml2的网络访问,这应该足

...

知道了,不知道它是如何无效的 - 证明文件: $xpath = new \DOMXpath($document);

$nodes = $xpath->query('//img[@id="banner"]');

// Return content if we don't have exactly one image with id="banner"

if(1 !== $nodes->length) return $content;

// DOMNode of the banner

$banner

...

用这个: $str = file_get_contents('http://dream-portal.net/index.php/board,65.0.html');

$doc = new DOMDocument();

@$doc->loadHTML($str);

$selector = new DOMXPath($doc);

foreach ($selector->query('//*[starts-with(@id, "msg_")]') as $node) {

var_dump

...

尝试创建文件的用户不是“yurow”(可能有权创建该文件的用户)。 相反,它是一个用户,如“apache”或“httpd”。 通常,系统设置为禁止apache / httpd用户在Web根目录中创建文件。 这是出于安全目的而做的,我不建议通过给webroot提供apache / httpd写访问来绕过它。 相反,您可以在/ home / yurow内部创建文档(不在/ home / yurow / wwwroot内)。 一个例子可能是:/home/yurow/xmldata/test02.xml。

...

尝试以正确的格式编写HTML,使用双引号分隔的属性值,而不是单引号,因此它们不会被编码。 Javascript识别由单引号分隔的字符串。 这是一个例子: $html = 'click here';

$doc = new DOMDocument();

$doc->loadHTML( $html );

echo $html . "\n";

echo "-----------------\n";

e

...

php domdocument getelementbyid,PHP DOMDocument- getElementByID添加Â代替空相关推荐

  1. mysql 长度为1 的空字符串_MYSQL,如果定义了一个字符串的字段,长度为255,那么当我添加一个空字符串数据时占用空间吗?...

    你的位置: 问答吧 -> PHP -> 问题详情 MYSQL,如果定义了一个字符串的字段,长度为255,那么当我添加一个空字符串数据时占用空间吗? MYSQL,如果定义了一个字符串的字段, ...

  2. 使用IHTMLDocument3 的getElementById获取控件总是返回空的解决方法

    调用的参数书写都正确,可是每次调用pElem都返回空指针? CComPtr<IDispatch> pDisp = (IDispatch*)GetDocument(); CComPtr< ...

  3. js getElementById().innerHTML和getElementById().value区别

    一般对于像表单元素input本身拥有value属性的元素(标签),才能使用getElementById().value获取value属性中的值 比如:<input type="text ...

  4. jeefast 添加非空判断

    实体类添加 /*** 班级名*/@NotBlank(message="班级名不能为空",groups= {AddGroup.class,UpdateGroup.class})//非 ...

  5. oracle如何添加非空约束,oracle 怎么用sql删除非空约束?

    最近在学习oracle的闪回知识(oracle11g),也注意到了这个问题,经初步测试是结果是这样的: 首先确认一下,数据库是否打开了闪回特性: show parameter recyclebin; ...

  6. Unity 在编辑器菜单栏里添加清除空文件夹

    在 Assets/App/Editor 路径下新建 ClearEmptyFolder 文件夹,并在该文件夹下新建文件 EmptyFolderClear.cs using System.IO; usin ...

  7. oracle数据库字符串添加空格,空字符串和空格字符串在informix和oralce 的差异

    空字符串和空格字符串在informix和oralce 的差异[@more@] informix:操作如下: > CREATE TABLE tt ( c1 int,c2 VARCHAR(8),UN ...

  8. html页面设置document类型,DOM-Document类型

    Document类型 JavaScript通过Document类型表示文档.在浏览器中,document对象是HTMLDocument(继承自Document类型)的一个实例,document对象是w ...

  9. PHP Fatal error: Class #39;DOMDocument#39; not found

     PHP Fatal error:  Class 'DOMDocument' not found 给PHP添加 xml模块: yum install php-xml 如果是PHP5,则用 yum in ...

最新文章

  1. linux操作系统的两种桌面环境,Linux操作系统中常见的桌面环境介绍
  2. android自定义xml弹窗,Android自定义弹窗提醒控件使用详解
  3. oracle数据库数据导入导出步骤(入门)
  4. mba案例分析_MBA小组面试案例分析你会吗?打开这份攻略让你一招制胜
  5. ExcelJS —— Node 的 Excel 读写扩展模块2
  6. 【渝粤教育】广东开放大学 软件工程 形成性考核 (50)
  7. [转]直接拿来用!最火的Android开源项目(一)
  8. jq中ajax请求跨域,用JQuery实现简单的Ajax跨域请求
  9. numpy 算术运算(Arithmetic operations)
  10. 期货品种产业链图表_农业
  11. 30个常用python实现
  12. mysql的数据库的索引_MySQL 数据库索引原理与分类
  13. c语言turboc图形代码,Turbo C使用第三方图形库
  14. 如何进行宽带测速,教程来啦!怎样在电脑上对宽带进行测速?
  15. java 保存图片_java后台接受到图片后保存方法
  16. 银行数字化转型导师坚鹏:《银行业同业竞争策略分析》
  17. 【模拟·习题】[USACO18JAN]Lifeguards S
  18. 和菲利普•科特勒的《营销管理》一样,这些都是比较好的市场营销书籍
  19. sed 删除/增加文件的某一行
  20. 【leetcode 993】【二叉树的堂兄弟节点】

热门文章

  1. spark restful_使用Spark构建简单的RESTful API
  2. 如何在ADF中将参数传递给ActionListener
  3. 5个令人震惊的统计数据证明日志不足
  4. 枚举集合的EnumSet
  5. apache camel_REST与Apache Camel
  6. 适用于Java开发人员的Elasticsearch:命令行中的Elasticsearch
  7. jpql hql_无需部署即可测试JPQL / HQL
  8. netbeans7.4_NetBeans 7.1:创建自定义提示
  9. 设计模式的Java 8 Lambda表达式–装饰器设计模式
  10. 带有Java 8,lambda表达式和Mockito-Java8附加组件的更紧凑的Mockito