$dom = new DOMDocument;
$dom->loadHTMLFile( "temp/page".$y.".xhtml" );
$xpath = new DOMXPath( $dom );
$pDivs = $xpath->query(".//div[@class='Areas']");
foreach ( $pDivs as $div ) {
$div->parentNode->removeChild( $div );
}
echo htmlspecialchars($dom->saveHTMLFile());
它不起作用......
我的html文件显示:
<html>
<head>
<title></title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div style="height:998px;">
<img src="images/bg004.jpg" />
<div class="class1">
<div class="class2"></div>
<div class="class2"></div>
</div>
<div class="Areas">
<div class="Area"><a href="index.html"></a></div>
<div class="Area"><a href="index.html"></a></div>
<div class="Area"><a href="index.html"></a></div>
</div>
</div>
</body>
</html>
我想得到这样的结果:
<html>
<head>
<title></title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div style="height:998px;">
<img src="images/bg004.jpg" />
<div class="class1">
<div class="class2"></div>
<div class="class2"></div>
</div>
<div class="Areas">
</div>
</div>
</body>
</html>
感谢您的帮助
更新
如何做同样的事情,但我的文件现在是xml?
我测试了这个:
copy("temp/page".$y.".xhtml", "/temp/page".$y.".xml");
$dom = new DOMDocument;
$dom->load( "temp/page".$y.".xml" );
$xpath = new DOMXPath( $dom );
$pDivs = $xpath->query(".//div[@class='Area']");
foreach ( $pDivs as $div ) {
$div->parentNode->removeChild( $div );
}
$dom->savexml();
现在我已经
了<?xml version="1.0" encoding="utf-8"?>
<html>
<head>
<title></title>
<link href="css.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div style="height:998px;">
<img src="images/bg004.jpg" />
<div class="class1">
<div class="class2"></div>
<div class="class2"></div>
</div>
<div class="Areas">
<div class="Area"><a href="index.html"></a></div>
<div class="Area"><a href="index.html"></a></div>
<div class="Area"><a href="index.html"></a></div>
</div>
</div>
</body>
</html>
答案 0 :(得分:4)
saveHTML
只需将html作为字符串输出saveHTMLFile
即可将其另存为文件。
答案 1 :(得分:4)
你几乎就在那里。您只需将Areas
更改为Area
,然后使用saveHtmlFile
代替saveHTML
:
$dom = new DOMDocument;
$dom->loadHTMLFile( "temp/page".$y.".xhtml" );
$xpath = new DOMXPath( $dom );
$pDivs = $xpath->query(".//div[@class='Area']");
foreach ( $pDivs as $div ) {
$div->parentNode->removeChild( $div );
}
$dom->saveHTMLFile("temp/page".$y.".xhtml");
这假设您要将HTML保存回原始文档。请注意,DOMXPath会在文档的顶部添加doctype,我认为没关系?
答案 2 :(得分:3)
您想要删除类Area
的div,只需更改XPath查询:
$pDivs = $xpath->query(".//div[@class='Area']"); // not 'Areas'
当然,您还需要对结果做一些事情,例如:
echo htmlspecialchars($dom->saveHTML()); // prints the result