PHP DOM RemoveChild不起作用

I have the following code:

<?php
$li = ('www.somesite.com');     
$ht = file_get_contents($li);
$dom = new DOMDocument();
libxml_use_internal_errors(true);
$dom->loadHTML($ht);
$divs = $dom->getElementsByTagName('section');
    foreach ($divs as $div){
        if(preg_match('/\btresc\b/', $div->getAttribute('id'))) {


            $chapter = $div->getElementsByTagName('div1')->item(0);

            $oldchapter = $div->removeChild($chapter);

            echo $oldchapter;
            }


        }


?>

I'm trying to remove <div class="div1">.*</div> with <section id="tresc">.*</section> However, I get the following error: Fatal error: Call to a member function removeChild() on a non-object. Does anybody know what I'm doing wrong here? Any help is greatly appreciated!

Don't include the quotes in your preg_match(), getAttribute() will just give you the plain value without quotes:

if(preg_match('/tresc/', $div->getAttribute('id'))) {

You should switch to simple string comparison instead as there is no real value in using a regex.

if($div->getAttribute('id') == 'tresc') {