1

What is wrong in this snippet code:

<?php
$DOMcontent = new \DOMDocument();
$newTag = new \DOMDocument();       

$DOMcontent->loadHTML($content);
$newTag->loadHTML('<link href="/compiled/styles.css" rel="stylesheet" type="text/css">');

$DOMcontent->getElementsByTagname('head')->item(0)->appendChild($DOMcontent->importNode($newTag));

where $content is an HTML string

I'm getting this error: Warning: DOMDocument::importNode(): Cannot import: Node Type Not Supported

Any ideas?

Edit:

I also tried @Hanshenrik option:

$DOMcontent->getElementsByTagname('head')->item(0)->appendChild($DOMcontent->imp‌​ortNode($newTag->cloneNode(true),true));

but now I'm getting:

Fatal error: Call to undefined method DOMDocument::imp‌​ortNode()
Manolo
  • 24,020
  • 20
  • 85
  • 130
  • i could guess, $DOMcontent->getElementsByTagname('head')->item(0)->appendChild($DOMcontent->importNode($newTag->cloneNode(true),true)); – hanshenrik Mar 16 '14 at 09:51
  • @hanshenrik - Now I'm getting: *Fatal error: Call to undefined method DOMDocument::imp‌​ortNode()* – Manolo Mar 16 '14 at 09:55
  • You are using `loadHTML` but only provide a partial document. The underlying libxml will then provide the basic HTML skeleton consisting of html, head and body tags around your partial content. So what your are trying to import there is a full fledged DOMDocument node. – Gordon Mar 16 '14 at 10:35

0 Answers0