title: php dom create element but self-closing
body: <p>I want to create a html block, like this:</p>
  
  <pre><code>&lt;media&gt;
      &lt;media-reference source='15.jpg' /&gt;
      &lt;media-caption&gt;caption&lt;/media-caption&gt;
      &lt;hasSyndicationRights&gt;1&lt;/hasSyndicationRights&gt;
      &lt;licenseId&gt;1&lt;/licenseId&gt;
      &lt;licensorName&gt;name&lt;/licensorName&gt;
  &lt;/media&gt;
  </code></pre>
  
  <p>But in my code <code>&lt;media-reference&gt;</code> closes like this <code>&lt;/media-reference&gt;</code>. </p>
  
  <p>How can I close just that tag?</p>
  
  <p>This is my code:</p>
  
  <pre><code>$valFieldBody = '&lt;p&gt;Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odio labore aut hic soluta! Animi quaerat unde commodi minus dicta, a quidem. Soluta quaerat delectus, id, dolor ex placeat molestiae quae.&lt;/p&gt;&lt;p&gt;&lt;img  height="500" src="15.jpg" width="500"/&gt;&lt;/p&gt;';
  $htmlEncoded = mb_convert_encoding($valFieldBody, 'HTML-ENTITIES', 'UTF-8');
  $doc = new DOMDocument;
  $opcionesLibXML = LIBXML_COMPACT | LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD;
  libxml_use_internal_errors(true);
  @$doc-&gt;loadHTML($htmlEncoded, $opcionesLibXML);
  libxml_use_internal_errors(false);
  
  $img_tag = $doc-&gt;getElementsByTagName('img');
  
  foreach ($img_tag as $key =&gt; $img_items){
      $img_src = $img_items-&gt;getAttribute('src');
      $tag_media = $doc-&gt;createElement('media');
      $tag_media_reference = $doc-&gt;createElement('media-reference');
      $tag_media_reference-&gt;setAttribute('mime-type','image/jpg');
      $tag_media_reference-&gt;setAttribute('source',$img_src);
      $tag_media_caption = $doc-&gt;createElement('media-caption',$img_title);
      $tag_hasSyndicationRights = $doc-&gt;createElement('hasSyndicationRights','1');
      $tag_licenseId = $doc-&gt;createElement('licenseId','1');
      $tag_licensorName = $doc-&gt;createElement('licensorName',$img_title);
      $tag_media-&gt;appendChild($tag_media_reference);
      $tag_media-&gt;appendChild($tag_media_caption);
      $tag_media-&gt;appendChild($tag_hasSyndicationRights);
      $tag_media-&gt;appendChild($tag_licenseId);
      $tag_media-&gt;appendChild($tag_licensorName);
      $img_items-&gt;parentNode-&gt;replaceChild($tag_media, $img_items);
  }
  $valFieldBody = $doc-&gt;saveHTML($doc-&gt;documentElement);
  </code></pre>
  
  <p>For now I'm using <code>str_replace</code> to change that:</p>
  
  <pre><code>$valFieldBody = str_replace("&gt;&lt;/media-reference&gt;"," /&gt;", $valFieldBody);
  </code></pre>