title: Adding style tags to head with PHP DOMDocument
body: <p>I want to create and add a set of <code>&lt;style&gt;</code> tags to the head tags of an HTML document.</p>
  
  <p>I know I can start out like this:</p>
  
  <pre><code>$url_contents = file_get_contents('http://example.com');
  $dom = new DOMDocument;
  $dom-&gt;loadHTML($url_contents);
  
  $new_elm = $dom-&gt;createElement('style', 'css goes here');
  $elm_type_attr = $dom-&gt;createAttribute('type');
  $elm_type_attr-&gt;value = 'text/css';
  $new_elm-&gt;appendChild($elm_type_attr);
  </code></pre>
  
  <p>Now, I also know that I can add the new style tags to the HTML like this:</p>
  
  <pre><code>$dom-&gt;appendChild($ss_elm);
  $dom-&gt;saveHTML();
  </code></pre>
  
  <p>However, this would create the following scenario:</p>
  
  <pre><code>&lt;html&gt;
  &lt;!--Lots of HTML here--&gt;
  &lt;/html&gt;&lt;style type="text/css"&gt;css goes here&lt;/style&gt;
  </code></pre>
  
  <p>The above is essentially pointless; the CSS is not parsed and just sits there.</p>
  
  <p>I found this solution online (obviously didn't work):</p>
  
  <pre><code>$head = $dom-&gt;getElementsByTagName('head');
  $head-&gt;appendChild($new_elm);
  $dom-&gt;saveHTML();
  </code></pre>
  
  <p>Thanks for the help!!</p>
  
  <h3>EDIT:</h3>
  
  <p>Is it possible?</p>