我正在尝试将同一div类与父div类合并。 试图匹配正则表达式http://www.regexr.com/3bim9。但是我对此并不擅长,我也没有得到任何关于如何做到这一点并思考大约4个小时。这也适用于多个内部div。我有预感,可以用preg_replace
完成当前输出:
<div class="dotted-highlight">
Here is a check list to determine whether.
<div class="dotted-highlight">
Tick all those apply to you. The more boxes you tick.
</div>
</div>
预期输出
<div class="dotted-highlight">
Here is a check list to determine whether.
Tick all those apply to you. The more boxes you tick.
</div>
PHP
<?php
$textarea = '<div class="dotted-highlight">
Here is a check list to determine whether.
<div class="dotted-highlight">
Tick all those apply to you. The more boxes you tick.
</div>
</div>';
$textarea = preg_replace('(<div class="dotted-highlight">){1,}(.*?)', '', $textarea);
?>
答案 0 :(得分:1)
以下是使用PHP DOM和XPath实现它的方法:
$html = "<<HTML STRING>>";
$dom = new DOMDocument;
$dom->loadHTML("<div id=\"tmptmptmp\">" . $html . "</div>", LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
$xp = new DOMXPath($dom);
$links = $xp->query('//div');
foreach ($links as $link) {
$class = $link->getAttribute('class');
$nested = $xp->query('.//div', $link);
foreach ($nested as $n) {
if ($n->getAttribute('class') == $class) {
//$html = $doc->getElementsByTagName("html")->item(0);
$fragment = $dom->createDocumentFragment();
while ($n->childNodes->length > 0) {
$fragment->appendChild($n->childNodes->item(0));
}
$n->parentNode->replaceChild($fragment, $n);
}
}
}
echo preg_replace('/^\s*<div\s+id="tmptmptmp">\s*|\s*<\/div>\s*$/', '', $dom->saveHTML());
请参阅IDEONE demo
请注意,preg_replace
仅用于对手动添加的<div>
进行后期处理,以帮助解决HTML代码不是正确的HTML文档时出现的问题(例如,没有<html>
和<body>
标签)。