title: PHP DOM replace element with a new element
body: <p>I have a DOM object with loaded HTML markup. I'm trying to replace all embed tags that look like this:</p>
  
  <pre><code>&lt;embed allowfullscreen="true" height="200" src="path/to/video/1.flv" width="320"&gt;&lt;/embed&gt;
  </code></pre>
  
  <p>With a tag like this:</p>
  
  <pre><code>&lt;a 
  href="path/to/video/1.flv" 
  style="display:block;width:320px;height:200px;" 
  id="player"&gt;
  &lt;/a&gt;
  </code></pre>
  
  <p>I'm having troubles figuring this out and I don't want to use regular expression for this. Could you help me out?</p>
  
  <p>EDIT:</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>         // DOM initialized above, not important
              foreach ($dom-&gt;getElementsByTagName('embed') as $e) {
                  $path = $e-&gt;getAttribute('src');
            $width = $e-&gt;getAttribute('width') . 'px';
            $height = $e-&gt;getAttribute('height') . 'px';
            $a = $dom-&gt;createElement('a', '');
            $a-&gt;setAttribute('href', $path);
            $a-&gt;setAttribute('style', "display:block;width:$width;height:$height;");
            $a-&gt;setAttribute('id', 'player');
            $dom-&gt;replaceChild($e, $a); // this line doesn't work
        }
  </code></pre>