Remove entire div tag contents turn into a function

118 views Asked by At

What I'm seeking to do is find an elegant solution to remove the contents of everything between a certain class = i.e. you want to remove all the HTML in the sometestclass class using php.

The function below works somewhat - not that well - it removes some parts of the page I don't want removed. Below is a function based on an original post (below):

$html = "<p>Hello World</p>
         <div class='sometestclass'>
           <img src='foo.png'/>
           <div>Bar</div>
         </div>";
$clean = removeDiv ($html,'sometestclass');
echo $clean;

function removeDiv ($html,$removeClass){
$dom = new DOMDocument;
$dom->loadHTML( $html );

$xpath = new DOMXPath( $dom );
$removeString = ".//div[@class='$removeClass']";
$pDivs = $xpath->query($removeString);

foreach ( $pDivs as $div ) {
  $div->parentNode->removeChild( $div );
}

$output = preg_replace( "/.*<body>(.*)<\/body>.*/s", "$1", $dom->saveHTML() );
return $output;
}

does anyone have any suggestions to improve the results of this?

the original post is here

1

There are 1 answers

5
jeroen On

You are not quoting the class name:

$removeString = ".//div[@class=$removeClass]";

should be:

$removeString = ".//div[@class='$removeClass']";