PHP DOM:不加载 css 样式
我有这段代码,它获取页面的 html 代码,然后替换 A' 标记的所有 HREF 属性以将其重定向到我的网站,然后我的网站加载页面并再次重定向链接等等...
<?php
libxml_use_internal_errors(true); // hide the parsing errors
$dom = new DOMDocument; // init new DOMDocument
if($_GET){
$dom->loadHtmlFile($_GET['open']); // getting link to redirect to
}else{
$dom->loadHtmlFile('http://www.stackoverflow.com'); // getting default site
}
$dom->loadHtmlFile('http://www.stackoverflow.com'); // load HTML into it
$xpath = new DOMXPath($dom); // create a new XPath
$nodes = $xpath->query('//a[@href]'); // Find all A elements with a href attribute
foreach($nodes as $node) { // Iterate over found elements
$node->setAttribute('href', 'index.php?open=http://www.stackoverflow.com'.$node->getAttribute('href')); // Change href attribute
}
echo $dom->saveXml(); // output cleaned HTML
?>
代码非常完美running ,唯一的问题是它不会以某种方式加载CSS文件.. 非常欢迎您测试这段代码,看看有什么问题!
这是在线版本:http://browser.breet.co.il
提前谢谢您!
I have this code which getting html code of page and than replace all the HREF attributes of the A' tag to redirect it to my site , than my site load the page and again redirect the links and so on...
<?php
libxml_use_internal_errors(true); // hide the parsing errors
$dom = new DOMDocument; // init new DOMDocument
if($_GET){
$dom->loadHtmlFile($_GET['open']); // getting link to redirect to
}else{
$dom->loadHtmlFile('http://www.stackoverflow.com'); // getting default site
}
$dom->loadHtmlFile('http://www.stackoverflow.com'); // load HTML into it
$xpath = new DOMXPath($dom); // create a new XPath
$nodes = $xpath->query('//a[@href]'); // Find all A elements with a href attribute
foreach($nodes as $node) { // Iterate over found elements
$node->setAttribute('href', 'index.php?open=http://www.stackoverflow.com'.$node->getAttribute('href')); // Change href attribute
}
echo $dom->saveXml(); // output cleaned HTML
?>
the code is perfectly running , the only problem is that it won't load CSS files somehow..
you're more than welcome to test this code and see what's the problems!
here is online version: http://browser.breet.co.il
thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 saveHTML() 而不是 saveXml()
使用最后一个,在打印代码的开头有一个 xml 定义,因此它不会解析正确。
Use saveHTML() instead of saveXml()
Using the last one, there's an xml definition at the start of the printed code so it doesn't parse correctly.