Naz*_*riy 28 html php dom domdocument
有没有什么方法可以在没有内容编码的情况下将HTML模板插入现有DOMNode?
我试过这样做:
$dom->createElement('div', '<h1>Hello world</h1>');
$dom->createTextNode('<h1>Hello world</h1>');
Run Code Online (Sandbox Code Playgroud)
输出几乎相同,唯一的区别是第一个代码将它包装在div中.我试图从字符串加载HTML,但我不知道如何将它的正文内容附加到另一个DOMDocument.
在javascript中,这个过程看起来非常简单明了.
Gor*_*don 41
您可以使用
例:
// just some setup
$dom = new DOMDocument;
$dom->loadXml('<html><body/></html>');
$body = $dom->documentElement->firstChild;
// this is the part you are looking for
$template = $dom->createDocumentFragment();
$template->appendXML('<h1>This is <em>my</em> template</h1>');
$body->appendChild($template);
// output
echo $dom->saveXml();
Run Code Online (Sandbox Code Playgroud)
输出:
<?xml version="1.0"?>
<html><body><h1>This is <em>my</em> template</h1></body></html>
Run Code Online (Sandbox Code Playgroud)
如果要从另一个DOMDocument导入,请将三行替换为
$tpl = new DOMDocument;
$tpl->loadXml('<h1>This is <em>my</em> template</h1>');
$body->appendChild($dom->importNode($tpl->documentElement, TRUE));
Run Code Online (Sandbox Code Playgroud)
使用TRUE第二个参数importNode将对节点树进行递归导入.
如果您需要导入(格式错误的)HTML,请更改loadXml为loadHTML.这将触发libxml的HTML解析器(ext/DOM在内部使用):
libxml_use_internal_errors(true);
$tpl = new DOMDocument;
$tpl->loadHtml('<h1>This is <em>malformed</em> template</h2>');
$body->appendChild($dom->importNode($tpl->documentElement, TRUE));
libxml_use_internal_errors(false);
Run Code Online (Sandbox Code Playgroud)
需要注意的是的libxml将设法改正的标记,例如,它会改变错误的收盘</h2>来</h1>.
Gum*_*mbo 28
它与另一个DOMDocument一起用于解析HTML代码.但您需要先将节点导入主文档,然后才能在其中使用它们:
$newDiv = $dom->createElement('div');
$tmpDoc = new DOMDocument();
$tmpDoc->loadHTML($str);
foreach ($tmpDoc->getElementsByTagName('body')->item(0)->childNodes as $node) {
$node = $dom->importNode($node, true);
$newDiv->appendChild($node);
}
Run Code Online (Sandbox Code Playgroud)
并作为一个方便的功能:
function appendHTML(DOMNode $parent, $source) {
$tmpDoc = new DOMDocument();
$tmpDoc->loadHTML($source);
foreach ($tmpDoc->getElementsByTagName('body')->item(0)->childNodes as $node) {
$node = $parent->ownerDocument->importNode($node, true);
$parent->appendChild($node);
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以简单地这样做:
$elem = $dom->createElement('div');
appendHTML($elem, '<h1>Hello world</h1>');
Run Code Online (Sandbox Code Playgroud)
因为我不想与XML斗争,因为它会更快地抛出错误而且我不喜欢为@添加前缀以防止错误输出.在我看来,loadHTML做得更好,而且非常简单:
$doc = new DOMDocument();
$div = $doc->createElement('div');
// use a helper to load the HTML into a string
$helper = new DOMDocument();
$helper->loadHTML('<a href="#">This is my HTML Link.</a>');
// now the magic!
// import the document node of the $helper object deeply (true)
// into the $div and append as child.
$div->appendChild($doc->importNode($helper->documentElement, true));
// add the div to the $doc
$doc->appendChild($div);
// final output
echo $doc->saveHTML();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25473 次 |
| 最近记录: |