PHP的DOM创建元素,但自我关闭

时间:2019-05-22 15:47:02

标签: php dom

我有一些问题 我想创建一个html块,像这样:

<media>
    <media-reference source='15.jpg' />
    <media-caption>caption</media-caption>
    <hasSyndicationRights>1</hasSyndicationRights>
    <licenseId>1</licenseId>
    <licensorName>name</licensorName>
</media>

但是在我的代码<media-reference>中像这样</media-reference>

我如何只关闭该标签。

这是我的代码:

          $valFieldBody = '<p>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.</p><p><img  height="500" src="15.jpg" width="500"/></p>';
          $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->loadHTML($htmlEncoded, $opcionesLibXML);
          libxml_use_internal_errors(false);

          $img_tag = $doc->getElementsByTagName('img');

          foreach ($img_tag as $key => $img_items){
                $img_src = $img_items->getAttribute('src');
                $tag_media = $doc->createElement('media');
                $tag_media_reference = $doc->createElement('media-reference');
                $tag_media_reference->setAttribute('mime-type','image/jpg');
                $tag_media_reference->setAttribute('source',$img_src);
                $tag_media_caption = $doc->createElement('media-caption',$img_title);
                $tag_hasSyndicationRights = $doc->createElement('hasSyndicationRights','1');
                $tag_licenseId = $doc->createElement('licenseId','1');
                $tag_licensorName = $doc->createElement('licensorName',$img_title);
                $tag_media->appendChild($tag_media_reference);
                $tag_media->appendChild($tag_media_caption);
                $tag_media->appendChild($tag_hasSyndicationRights);
                $tag_media->appendChild($tag_licenseId);
                $tag_media->appendChild($tag_licensorName);
                $img_items->parentNode->replaceChild($tag_media, $img_items);
          }
          $valFieldBody = $doc->saveHTML($doc->documentElement);

现在我使用str_replace进行更改:

$valFieldBody = str_replace("></media-reference>"," />", $valFieldBody);

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

问题是saveHTML()使用它自己的规则创建输出(据我所知),并且并不总是按照XHTML标准编写东西。相反,如果您使用saveXML()将其写出,则应该获得更标准的输出

$valFieldBody = $doc->saveXML($doc->documentElement);

这给...

<p>
    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.
    <p>
        <media>
            <media-reference mime-type="image/jpg"
                source="15.jpg" />
            <media-caption>abcd</media-caption>
            <hasSyndicationRights>1</hasSyndicationRights>
            <licenseId>1</licenseId>
            <licensorName>abcd</licensorName>
        </media>
    </p>
</p>

(请注意,如果原始来源不是XHTML,另存为XML实际上可能会导致其他问题)