Skip to content Skip to sidebar Skip to footer

Find And Edit Comment Element Of Html With Php

I need to edit the inside of the following comment tag so that I can change the location of the css file. This needs to be done through PHP. I have tried using the following code b

Solution 1:

Try "//comment()" to get all CommentNodes.

Note that the link element in the CommentNode is not a childNode of the commentNode but mere data:

libxml_use_internal_errors(TRUE);
$dom = new DOMDocument;
$dom->loadHTMLFile( 'http://www.nytimes.com/' );
libxml_clear_errors();

$xpath = new DOMXPath($dom);
foreach( $xpath->query('//comment()[contains(., "link")]') as$node)
{
   var_dump( $node->data );
}

This will give:

string(135) "[if IE]>
    <link rel="stylesheet" type="text/css" href="http://graphics8.nytimes.com/css/0.1/screen/build/homepage/ie.css">
<![endif]"

string(138) "[if IE 6]>
    <link rel="stylesheet" type="text/css" href="http://graphics8.nytimes.com/css/0.1/screen/build/homepage/ie6.css">
<![endif]"

As you can see, the actual comment node is just the <!-- and --> parts. The remainder is just text. The [if IE]> is not part of the actual tag. It is character data.

Post a Comment for "Find And Edit Comment Element Of Html With Php"