1

I have an HTML page I'd like to edit. I want to remove a certain section of the .html file such as.

<div id="gg">
......
......
</div>

How can I do this?

imm
  • 5,837
  • 1
  • 26
  • 32
Pulseczar
  • 67
  • 6

3 Answers3

3

I would recommend using PHP's DOM library:

$dom = new DOMDocument;
$dom->loadHTML('<html string />'); // Or $dom->loadHTMLFile('file_name.html');

$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//div[id="gg"]');
if($nodes->length)
  $nodes[0]->parentNode->removeChild($nodes[0]);

$dom->saveHTML(); // Or $dom->saveHTMLFile('file_name.html');
connec
  • 7,231
  • 3
  • 23
  • 26
1
$file = file_get_contents("index.html");
$file = preg_replace('/<div id="gg">.*?<\/div>/im', '' $file);
file_put_contents($file);

I not tested this code.

Attention: nested divs break html structure.

neworld
  • 7,757
  • 3
  • 39
  • 61
1

You might have some luck using an XML (or HTML) parser. This one for PHP 5 looks really easy to use, and provides a mechanism for finding a particular element by ID and then setting its contents to an empty string.

imm
  • 5,837
  • 1
  • 26
  • 32