Hi I wanna remove a line from a HTML file with PHP like this:
<div id="buttons">
<div id="buttonid_4"><a href="#">Button 4</a></div>
<div id="buttonid_3"><a href="#">Button 3</a></div>
<div id="buttonid_2"><a href="#">Button 2</a></div>
<div id="buttonid_1"><a href="#">Button 1</a></div>
</div>
So, I wanna remove the buttonid_4, and it content. That it will be like this:
<div id="buttons">
<div id="buttonid_3"><a href="#">Button 3</a></div>
<div id="buttonid_2"><a href="#">Button 2</a></div>
<div id="buttonid_1"><a href="#">Button 1</a></div>
</div>
First I think it is easy, but I can't found the answer :|
I tried: "as simple"
$dom = new DOMDocument('1.0', 'utf-8');
$dom->loadHTMLFile($The_Path_For_File);
$element = $dom->getElementById('buttonid_'. $Button_Id);
$element->parentNode->removeChild($element);
$dom->saveHTMLFile($The_Path_For_File);
I got
Call to a member function removeChild() on a non-object
and everytime when I tried with GetElementById, so I continue with XPATH:
$xpath = new DOMXpath($dom);
$nodeList = $xpath->query('//div[@id="buttonid'.$Button_Id.'"]');
foreach($nodeList as $element){
$dom->$element->removeChild($element);
}
$dom->saveHTMLFile($The_Path_For_File);
I didn't get error, the notepad requested the refresh for file, but no change Anyone know how to produce this?