tags:

views:

29

answers:

1

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 but I get invalid expression errors.

Comment Section:

<!--[if IE 6]><link rel="stylesheet" type="text/css" href="/Css/IE6.css" media="screen" /><![endif]-->

Code that does not work:

    $csslist = $xpath->query("//<!--[if IE 6]>");    
foreach($csslist as $css) {                 
    $css->innertext = 'test';
}
+2  A: 

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"&gt;
<![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"&gt;
<![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.

Gordon
Thanks I can get that list now, but would there be a way to get only the specific comment I want? I don't need to actually edit it, I was intending on just replacing the text with new text. I think this can be done with innerHTML?
Jeremy
Thanks Gordon I will try this now.
Jeremy
@Gordon When I run my code and use '[if IE 6]' as the value of contain, the text I get back when I say echo $node->nodeValue; is only [if IE 6]>. I dont get anything between the brackets (ie what I want to change).
Jeremy
@Jeremy The code above uses $node->data, not ->nodeValue.
Maerlyn
@Jeremy I get the all content with `nodeValue`, but please try `var_dump($node->hasChildNodes())` to see if your version of libxml parses the link as an actual node (mine doesnt). If TRUE, change the XPath to '//comments/link' or get the childNode from the `$node`. If FALSE, try `$node->data` instead of `nodeValue`.
Gordon
@Gordon, With your exact code, I get the following output:string(135) "[if IE]> <![endif]" string(138) "[if IE 6]> Instead of anydata. This is using your code copy and pasted from above. Could this be to do with my version of php? I have the latest from memory. I get FALSE when using var_dump($node->hasChildNodes())
Jeremy
@Jeremy string(135) means there is 135 chars in the string. The bit you show is far less than that, which gets me to the conclusion you are using the browser to display the results. The browser will interprete the link. Have a look at the sourcecode of the page. I bet it's there.
Gordon
Hi Gordon, I didn't know that was the problem and stupidly didn't check the source after I had made the edit. I can now see that it does indeed work. Thanks heaps for you assistance!
Jeremy