好的,这就是我所取得的成就。虽然这可能不是最有效的方法,但我会分享。我使用了这里介绍的 DOMinnerHTML()和 preg_split()。这将删除最后三个<br>
标签之后的文本。
<?php
$html = <<<STR
<div>
<div><img sec=""></div>
<br>
<h3>title</h3>
<span>some text here</span>
<br>
Some text that I want to remove.
<br>
<br>
</div>
STR;
$doc = new DOMDocument;
$doc->loadHTML($html);
$node = $doc->getElementsByTagName('div')->item(0);
$innerHtml = DOMinnerHTML($node);
$arrHtml = preg_split('/<br.*?\/?>/i', $innerHtml); // devide the string into arrays by <br> or <br />
array_splice($arrHtml, -3); // remove the last three elements
$edited = implode(" ", $arrHtml);
echo $edited;
function DOMinnerHTML($element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
$innerHTML.=trim($tmp_dom->saveHTML());
}
return $innerHTML;
}
?>