-1

I have the following HTML code:

<div id="hero_techSpec">
    <div class="hero_techSpecItem">
        blubb
    </div>
</div>

Now, I'm trying to remove the inner div-element with "simple HTML DOM parser".

$document = HtmlDomParser::str_get_html("...some HTML code as string...");
$techSpec = $document->find("#hero_techSpec", 0);
echo $techSpec;

$techSpec->find(".hero_techSpecItem", 0)->outertext = '';

echo $techSpec;
$document->load($document->save());
echo $document->find("#hero_techSpec", 0); die;

In all three "echo"s, the inner div is still present. I tried to follow the related solution: Simple HTML Dom: How to remove elements?

However, it seems it is not working in my case. Do you have any ideas / hints how to solve that issue? Thank you!

  • Read that referenced question again. One answer suggests to use `outerhtml` instead of `outertext`, another references `removeNode()`. Something else to consider is maybe to use PHP's native HTML parsing library. It looks like it has all the functionality you need. – Quasipickle Aug 12 '21 at 20:58
  • thank you very much for your quick response. Neither outertext nor outerhtml is working. The result is exactly the same. Using removeNode() I'm getting the error of an undefined function. – user3731513 Aug 13 '21 at 20:05
  • Are you using the latest version? That library was last published in 2019, the same year the `removeNode()` answer was given. – Quasipickle Aug 13 '21 at 20:38
  • I'm using 1.9.1, which seems to be the latest version. The function "removeNode" is not listed in https://simplehtmldom.sourceforge.io/manual_api.htm – user3731513 Aug 13 '21 at 21:11

1 Answers1

0

Try something like this:

$document->load($htmlString);
$techSpec = $document->find(".//div[@class='hero_techSpecItem']")[0];
$techSpec->outertext  = "";
$document->load($document->save());
echo $document;

Output should be:

<div id="hero_techSpec">      </div>
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45