我使用 simple_html_dom.php
我想删除元素的第一个子元素:
的HTML:
<div id="result">
<a class="result_type1" href="#">The title</a>
<span class="item">item</span>
<span class="more">more</span>
<span class="description">description</span>
</div>
PHP 测试 1:
foreach($html2->find("div[id=result]") as $element)
{
$element->children(0)->outertext=$element->children(1)->outertext;
$element->children(1)->outertext=$element->children(2)->outertext;
$element->children(2)->outertext=$element->children(3)->outertext;
$element->children(3)->outertext="";
echo $element->children(0)->plaintext;
}
结果(不好):
The title
PHP 测试 2
foreach($html2->find("div[id=result]") as $element)
{
$element->children(0)->outertext=$element->children(1)->outertext;
$element->children(1)->outertext=$element->children(2)->outertext;
$element->children(2)->outertext=$element->children(3)->outertext;
$element->children(3)->outertext="";
echo $element->children(0)->outertext;
}
结果(好):
<span class="item">item</span>
而且我没有得到相同的项目。使用纯文本,我得到了应该删除的元素