2013-09-21 75 views
0

我使用xpath在样式表中更改样式表<link>的样式表hrefPHP XPath更改样式表

但它根本不起作用。

$html=file_get_contents('http://stackoverflow.com'); 
$doc = new DOMDocument(); 
$doc->loadHTML($html); 
$xpath = new DOMXPath($doc); 
$css_links = $xpath->evaluate("//link[@type='text/css']"); 
for ($i = 0; $i < $css_links->length; $i++) 
{ 
    $csslink = $css_links->item($i); 
    $oldurl = $csslink->getAttribute('href'); 
    $newURL='http://example.com/aaaa.css'; 
    $csslink->removeAttribute('href'); 
    $csslink->setAttribute('href', $newURL); 
} 
echo $html; 
+0

是[fopen封装(http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-:

您还可以foreach因为DOMNodeList implements Traversable更换for($i...) url-fopen)启用来从外部来源获取内容?为什么你用@ -notation压制错误信息?也许有你的答案。 – Mamuz

+0

@ mamuz,是的,根据phpinfo它是在 – werva

回答

1

您使用@$doc->loadHTML(html);代替@$doc->loadHTML($html);(注意$),否则它的工作原理。

还使用echo $doc->SaveHtml()而不是回显$html

工作示例here

foreach ($css_links as $csslink) 
{ 
    $oldurl = $csslink->getAttribute('href'); 
+0

谢谢,更正。仍然'echo $ css_links-> length;'显示0 – werva