PHP - Deleting content of div doesn't work in specific file -


i wrote function opens html file, deletes content of divs class , saves file. created 3 testfiles in 3 different locations. function works great on site.html , site3.html, not on site2.html

the folder structure following:

/testing/script.php // contains function

  • /testing/site.html
  • /testing/folder1/site2.html // script doesn't work in file
  • folder2/site3.html

the html of files identical

<!doctype html public "-//w3c//dtd html 4.0 transitional//en" "http://www.w3.org/tr/rec-html40/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> </head> <body> <div class="string">content of site2.html</div> <div id="well">not here</div> <div id="empty"></div> <div>foo</div> </body> </html> 

this script.php

<?php       $urls = array(             "site.html",             "/folder1/site2.html",             "../folder2/site3.html"         ); // urls of files      function deletecontent () {          global $urls;          echo "deletecontent running";          if (is_array($urls) || is_object($urls)) {             foreach($urls $url) {                  $dom = new domdocument;                 $dom->loadhtmlfile($url);                 $xpath = new domxpath($dom);                 $pdivs = $xpath->query(".//div[@class='string']");                  foreach ( $pdivs $div ) {                     $div->removechild($div->firstchild);                 }                  $dom->savehtmlfile($url);              }         }     }      deletecontent();   ?> 

i error

catchable fatal error: argument 1 passed domnode::removechild() must instance of domnode, null given, called in /testing/script.php on line 57 , defined in /testing/script.php on line 29

// line 29 in original file (adjusted question)

foreach ( $pdivs $div ) {     $div->removechild($div->firstchild); // line 29 } 

all can suggest testing firstchild before removing it, because code otherwise seems fine.

foreach ( $pdivs $div ) {     if( $div->firstchild )     {       $div->removechild( $div->firstchild );     } } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -