2013-09-21 74 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