Adding conditional comments using DomDocument

I saw a thread related to changing conditional comments, but I cannot find out if it is possible to add new conditional comments using php dom functionality.

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

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

I watched DomComment, but it seems to add a closing tag for the comment, so I end up with:

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

<?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 correct syntax for conditional commentary

<!--[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.

+3
source

Source: https://habr.com/ru/post/1791985/


All Articles