PHP DOM RemoveChild doesn't work -


i have 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 $div){         if(preg_match('/\btresc\b/', $div->getattribute('id'))) {               $chapter = $div->getelementsbytagname('div1')->item(0);              $oldchapter = $div->removechild($chapter);              echo $oldchapter;             }           }   ?> 

i'm trying remove <div class="div1">.*</div> <section id="tresc">.*</section> however, following error: fatal error: call member function removechild() on non-object. know i'm doing wrong here? appreciated!

don't include quotes in preg_match(), getattribute() give plain value without quotes:

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

you should switch simple string comparison instead there no real value in using regex.

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

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -