我想创建一个 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);
万千封印