Pulseczar
Pulseczar

Reputation: 67

Use PHP to delete section of .html file

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?

Upvotes: 1

Views: 1254

Answers (3)

connec
connec

Reputation: 7421

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');

Upvotes: 3

imm
imm

Reputation: 5919

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.

Upvotes: 1

neworld
neworld

Reputation: 7793

$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.

Upvotes: 1

Related Questions