如何使用 php 在 html 中的特定元素之后插入新元素?
How to insert new element after specific element in html using php?
我真的很难理解 DOMDocument
解析。
我正在尝试解决以下问题。
鉴于以下 HTML
<h1>Title</h1>
<p>Some Content</p>
<p>Some More Content</p>
<p>Other Content</p>
<p>Last Bit of Content</p>
我想在第二段标记后添加一个 div 和其他内容。
本质上,结果需要如下所示
<h1>Title</h1>
<p>Some Content</p>
<p>Some More Content</p>
<div>Something different</div> <!-- new tag -->
<p>Other Content</p>
<p>Last Bit of Content</p>
看了这里的一些 post 我现在已经厌倦了挠头。
您需要使用 DOMDocument
class to parsing string to html. After parsing html, select third p
element and use DOMNode::insertBefore
在其后插入新元素。
$doc = new DOMDocument();
$doc->loadHTML($html);
// find 3th p tag
$p = $doc->getElementsByTagName("p")->item(2);
// create new div tag
$div = $doc->createElement("div", "Something different");
// insert created element after 3th p
$p->parentNode->insertBefore($div, $p);
$html = $doc->saveHTML();
作为对@Mohammad 回答(完全正确)的补充,如果你想精确地找到 h1 标签之后的第二个 p 标签,并且在同一级别,你可以使用 XPath 查询 //h1/following-sibling::p[2]
。示例:
$html = <<<'EOD'
<h1>Title</h1>
<p>Some Content</p>
<p>Some More Content</p>
<p>Other Content</p>
<p>Last Bit of Content</p>
EOD;
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML('<div>' . $html . '</div>', LIBXML_HTML_NOIMPLIED);
libxml_clear_errors();
$xp = new DOMXPath($dom);
$targetNode = $xp->query('//h1/following-sibling::p[2]');
if ($targetNode->length) {
$targetNode = $targetNode->item(0);
$newNode = $dom->createElement('div', 'something different');
$targetNode->parentNode->insertBefore($newNode, $targetNode->nextSibling);
$targetNode->parentNode->insertBefore($dom->createTextNode("\n"), $targetNode->nextSibling);
}
$result = '';
foreach ($dom->documentElement->childNodes as $childNode) {
$result .= $dom->saveHTML($childNode);
}
echo $result;
我真的很难理解 DOMDocument
解析。
我正在尝试解决以下问题。
鉴于以下 HTML
<h1>Title</h1>
<p>Some Content</p>
<p>Some More Content</p>
<p>Other Content</p>
<p>Last Bit of Content</p>
我想在第二段标记后添加一个 div 和其他内容。 本质上,结果需要如下所示
<h1>Title</h1>
<p>Some Content</p>
<p>Some More Content</p>
<div>Something different</div> <!-- new tag -->
<p>Other Content</p>
<p>Last Bit of Content</p>
看了这里的一些 post 我现在已经厌倦了挠头。
您需要使用 DOMDocument
class to parsing string to html. After parsing html, select third p
element and use DOMNode::insertBefore
在其后插入新元素。
$doc = new DOMDocument();
$doc->loadHTML($html);
// find 3th p tag
$p = $doc->getElementsByTagName("p")->item(2);
// create new div tag
$div = $doc->createElement("div", "Something different");
// insert created element after 3th p
$p->parentNode->insertBefore($div, $p);
$html = $doc->saveHTML();
作为对@Mohammad 回答(完全正确)的补充,如果你想精确地找到 h1 标签之后的第二个 p 标签,并且在同一级别,你可以使用 XPath 查询 //h1/following-sibling::p[2]
。示例:
$html = <<<'EOD'
<h1>Title</h1>
<p>Some Content</p>
<p>Some More Content</p>
<p>Other Content</p>
<p>Last Bit of Content</p>
EOD;
libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML('<div>' . $html . '</div>', LIBXML_HTML_NOIMPLIED);
libxml_clear_errors();
$xp = new DOMXPath($dom);
$targetNode = $xp->query('//h1/following-sibling::p[2]');
if ($targetNode->length) {
$targetNode = $targetNode->item(0);
$newNode = $dom->createElement('div', 'something different');
$targetNode->parentNode->insertBefore($newNode, $targetNode->nextSibling);
$targetNode->parentNode->insertBefore($dom->createTextNode("\n"), $targetNode->nextSibling);
}
$result = '';
foreach ($dom->documentElement->childNodes as $childNode) {
$result .= $dom->saveHTML($childNode);
}
echo $result;