Use PHP to delete section of .html file
I have an HTML page I'd like to edit. I want to rem开发者_StackOverflow社区ove a certain section of the .html file such as.
<div id="gg">
......
......
</div>
How can I do this?
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');
$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.
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.
精彩评论