0

I use xpath to change stylesheet of href of stylesheet <link> in header.

But it doesn't work at all.

$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;
2
  • Is fopen wrappers enabled to get content from external source? And why do you suppress error message with @-notation? Maybe there is your answer. Commented Sep 21, 2013 at 20:36
  • @mamuz,yes, according to phpinfo it is on Commented Sep 21, 2013 at 20:38

1 Answer 1

1

You're using @$doc->loadHTML(html); instead of @$doc->loadHTML($html); (note the $), otherwise it works.

Also use echo $doc->SaveHtml() instead of echoing $html.

Working example here.

You also can replace for($i...) with foreach because DOMNodeList implements Traversable:

foreach ($css_links as $csslink) 
{
    $oldurl = $csslink->getAttribute('href');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.