您绝对最好的选择,imo 将使用 PHP 的原生 DOMDocument:http://php.net/manual/en/class.domdocument.php
这方面的学习曲线相当长,所以我想出了一些可以让你朝着正确方向前进的东西——如果不能完全提供解决方案的话。我在这里逐行包含 cmets 来解释每个步骤:
// the filename you want to parse
$filename = './test.html';
// an array of replacement html snippets and the id of the child element.
// html will be inserted before parent of each child with a matching ID as you described
$replacements = [
[
'id' => 'myId',
'insert' => '<button>Insert before parent of #myId</button>'
],
[
'id' => 'myId2',
'insert' => '<button>Insert before parent of#myId2</button>'
]
];
// instantiate DOMDocument and read the html file
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTMLFile('./test.html');
// get an array of all dom elements
$elements = $dom->getElementsByTagName('*');
// iterate through dom elements
foreach($elements as $element) {
// check if this element has an 'id' attribute
if ($element->hasAttribute('id')) {
// iterate through replacement array
foreach ($replacements as $i => $replacement) {
// if element's id is a match then add this node to our array
if ($element->getAttribute('id') == $replacement['id']) {
$replacements[$i]['nodes'][] = $element;
}
}
}
}
// iterate through replacements again
foreach ($replacements as $replacement) {
// iterate through nodes we found which matched
foreach ($replacement['nodes'] as $node) {
// create a DOMDocument node from an html string
$html = $dom->createDocumentFragment();
$html->appendXML($replacement['insert']);
// insert this node before parent
$node->parentNode->parentNode->insertBefore($html,$node->parentNode);
}
}
// output the revised html
echo $dom->saveHTML();
// note - if your html doesn't have <html> and <body> tags they will be automatically added by DOMDOcument
// you can work around this and get only body innerhtml with something like this
echo str_replace(['<body>','</body>'],'',$dom->saveHTML($dom->getElementsByTagName('body')->item(0)));
我使用以下名为 test.html 的 html 创建了一个测试文档。我故意使用myId 两次来证明这实际上将匹配每个元素,而不管其有效性:
<div> // no tags no info just a simple div
<div id="myId">
... some html
</div>
</div>
<div> // no tags no info just a simple div
<div id="myId2">
... some html
</div>
</div>
<div> // no tags no info just a simple div
<div id="myId">
... some html
</div>
</div>
上面的php代码输出如下:
<button>Insert before parent of #myId</button><div> // no tags no info just a simple div
<div id="myId">
... some html
</div>
</div>
<button>Insert before parent of #myId2</button><div> // no tags no info just a simple div
<div id="myId2">
... some html
</div>
</div>
<button>Insert before parent of #myId</button><div> // no tags no info just a simple div
<div id="myId">
... some html
</div>
</div>