php DOMDocument元素值不能为HTML [英] php DOMDocument element value can't be HTML

查看:42
本文介绍了php DOMDocument元素值不能为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是与问题相关的代码:



$ prep =< select>< option>选项1< / option> ;<选择的选项>选项1< / option>< / select>

$ td = $ dom-> createElement('td',$ prep);



解决方案:

  $ f = $ this-&dom; dom -> createDocumentFragment(); 
$ f-> appendXML($ prep);

但是仍然是一个大问题。没有值的任何属性,例如:已选择已禁用(不能将其写为 selected = selected createElement 不允许您这样做。



如何使用没有值的属性,而不像现在这样获得1000错误:
警告:DOMDocumentFragment :: appendXML()[domdocumentfragment.appendxml]:实体:第1行:解析器错误:规范要求所选属性的值
警告:DOMDocumentFragment :: appendXML()[domdocumentfragment.appendxml]:实体:第2行:解析器错误:块平衡不佳



在传递 selected 属性或将其禁用时会发生这种情况。

解决方案

我无法抗拒校正。在PHP的DOMDocument模型实现中,字符串也是也是对象(这使您可以轻松地选择它们,而不必依赖nodeValue,这使大多数情况下的工作变得容易)。 / p>

要插入带有文本的表格行,请执行以下操作:

  $ f =新的DOMDocument(); 
$ table = $ f-> createElement( table);
$ f-> appendChild($ table);
$ tbody = $ f-> createElement( tbody);
$ table-> appendChild($ tbody);
$ tr = $ f-> createElement( tr);
$ tbody-> appendChild($ tr);
$ td = $ f-> createElement( td);
$ tr-> appendChild($ td);

//魔术发生在这里
$ td-> appendChild(new DOMText( this is my text)));

正确插入时,保留空属性-但您可以自由使用selected = 选中,只要其内容是其属性名称的小写版本(当属性为布尔型属性时,在HTML 5中是什么意思?)。如果您仍希望仅将其标记为布尔值true,请使用:

  $ elem-> setAttribute( selected ,); 

(我只运行了代码+模式,没有任何错误,无需使用闭包)


This is the code related to the problem:

$prep = "<select><option>Option 1</option><option selected>Option 1</option></select>"
$td = $dom->createElement('td',$prep);

Solution:

$f = $this->dom->createDocumentFragment();
$f->appendXML($prep);

BUT still a big problem. Any attributes without value ex: selected, disabled (which you can't write as selected="selected") the createElement doesn't allow you to do so.

How can use attributes without value, and not get 1000 erros like now: Warning: DOMDocumentFragment::appendXML() [domdocumentfragment.appendxml]: Entity: line 1: parser error : Specification mandate value for attribute selected Warning: DOMDocumentFragment::appendXML() [domdocumentfragment.appendxml]: Entity: line 2: parser error : chunk is not well balanced

This happens when passing a selected attribute, or disabled.

解决方案

I cannot resist correcting. In PHP's implementation of the DOMDocument model, strings are also objects (this allows you to easily select them rather than to rely on nodeValue, which makes life easier in most cases).

To insert a table row with text, do this:

$f = new DOMDocument();
$table = $f->createElement("table");
$f->appendChild($table);
$tbody = $f->createElement("tbody");
$table->appendChild($tbody);
$tr = $f->createElement("tr");
$tbody->appendChild($tr);
$td = $f->createElement("td");
$tr->appendChild($td);

// Magic happens here
$td->appendChild(new DOMText("this is my text"));

Empty attributes are conserved when inserted properly, by the way - but you're free to use selected="selected", it is still valid provided that its content is the lowercase version of its attribute name ( What does it mean in HTML 5 when an attribute is a boolean attribute? ). If you'd still prefer to just flag it as boolean true, use:

$elem->setAttribute("selected","");

(I just ran the code + mode and got no errors whatsoever without the need to use shutop)

这篇关于php DOMDocument元素值不能为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆