Adding Conditional Comments using DomDocument

591 views Asked by At

I've seen a thread relating to altering conditional comments, but I can't find whether it's possible to add new conditional comments using the php dom functionality.

I essentially want to be able to add the following into (not the only example, but you get the idea!).

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->

I've looked at DomComment but it seems to add the closing tag for the comment, so I end up with:

<!--[if ! lte IE 6]--><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->
1

There are 1 answers

0
ax. On BEST ANSWER

this:

<?php
$doc = new DOMDocument();
$doc->loadHTML("<html><body><p id='bla'>Test</body></html>");
$bla = $doc->getElementById("bla");
$bla->appendChild(new DOMComment('[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]'));

echo $doc->saveHTML();  //<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]-->

works for me. note that the proper syntax for a conditional comment is

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><![endif]-->

not

<!--[if ! lte IE 6]><link rel="stylesheet" href="css/default.css" /><!--<![endif]-->

as you say you want to have it.