我有这个html结构; 然后我需要删除所有内容
<span class="Title_Blue">
,或者至少将代码块放入div中。 (然后我可以操纵它。)
反正我可以用xpath吗?
我可以轻松地将标签放在div中,但是这里的问题是我要包含到div中的代码都处于同一级别。
谢谢!
<body>
<div class="article_text">
<div id="text">
<p class="paragraph">
<strong> [text] </strong>
</p>
<div>
<strong> [text] </strong>
</div>
<strong>
<br> [Title] <br>
</strong>
<div style="text-align: justify;">
<br> [text]
</div>
<div style="text-align: justify;">
<br> [text]
</div>
<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<span class="Title_Blue"> [text] </span>
<br>
<p> ... </p>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
<div style="text-align: justify;"> [text] </div>
</div>
</div>
</body>
编辑
我尝试过:
$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"]');
foreach ($contents as $content) {
$div = $doc->createElement('div');
$div->setAttribute('class', 'DELETE_ME');
$content->parentNode->replaceChild($div, $content);
$div->appendChild($content);
}
编辑2
可以使用以下代码解决:
$html = $data
$doc = new DOMDocument();
@$doc->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xpath = new DOMXpath($doc);
$contents = $xpath->query('//*[@class="Title_Blue"] | //*[@class="Title_Blue"]/following-sibling::*');
foreach ($contents as $content) {
$content->parentNode->removeChild($content);
}
感谢安德森!
答案 0 :(得分:1)
如果您需要选择span
之后的所有内容,包括span
,请尝试
//span[@class="Title_Blue"]/preceding-sibling::*[1]/following-sibling::*