appendXML 剥离 img 元素
appendXML stripping out img element
我需要在文章中间插入带有 div 元素的图像。该页面是使用 CRM 中的 PHP 生成的。我有一个例程来计算所有段落标签的字符数,并在第 120 个字符的段落后插入 HTML。我正在使用 appendXML
并且它有效,直到我尝试插入图像元素。
当我放入<img>
元素时,它被剥离了。我知道它正在寻找 XML,但是,我正在关闭我认为会有所帮助的 <img>
标签。
有没有办法使用 appendXML
而不删除 img 元素?
$mcustomHTML = "<div style="position:relative; overflow:hidden;"><a href="https://example.com/code=123456"><img src="https://s3.amazonaws.com/a.example.com/image.png" alt="No image" /></img></a></div>";
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $content);
// read all <p> tags and count the text until reach character 120
// then add the custom html into current node
$pTags = $doc->getElementsByTagName('p');
foreach($pTags as $tag) {
$characterCounter += strlen($tag->nodeValue);
if($characterCounter > 120) {
// this is the desired node, so put html code here
$template = $doc->createDocumentFragment();
$template->appendXML($mcustomHTML);
$tag->appendChild($template);
break;
}
}
return $doc->saveHTML();
这应该适合你。它使用临时 DOM 文档将您拥有的 HTML 字符串转换为可用的内容。然后我们将临时文件的内容导入到主文件中。一旦它被导入,我们就可以像任何其他节点一样简单地附加它。
<?php
$mcustomHTML = '<div style="position:relative; overflow:hidden;"><a href="https://example.com/code=123456"><img src="https://s3.amazonaws.com/a.example.com/image.png" alt="No image" /></a></div>';
$customDoc = new DOMDocument();
$customDoc->loadHTML($mcustomHTML, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$doc = new DOMDocument();
$doc->loadHTML($content);
$customImport = $doc->importNode($customDoc->documentElement, true);
// read all <p> tags and count the text until reach character 120
// then add the custom html into current node
$pTags = $doc->getElementsByTagName('p');
foreach($pTags as $tag) {
$characterCounter += strlen($tag->nodeValue);
if($characterCounter > 120) {
// this is the desired node, so put html code here
$tag->appendChild($customImport);
break;
}
}
return $doc->saveHTML();
我需要在文章中间插入带有 div 元素的图像。该页面是使用 CRM 中的 PHP 生成的。我有一个例程来计算所有段落标签的字符数,并在第 120 个字符的段落后插入 HTML。我正在使用 appendXML
并且它有效,直到我尝试插入图像元素。
当我放入<img>
元素时,它被剥离了。我知道它正在寻找 XML,但是,我正在关闭我认为会有所帮助的 <img>
标签。
有没有办法使用 appendXML
而不删除 img 元素?
$mcustomHTML = "<div style="position:relative; overflow:hidden;"><a href="https://example.com/code=123456"><img src="https://s3.amazonaws.com/a.example.com/image.png" alt="No image" /></img></a></div>";
$doc = new DOMDocument();
$doc->loadHTML('<?xml encoding="utf-8" ?>' . $content);
// read all <p> tags and count the text until reach character 120
// then add the custom html into current node
$pTags = $doc->getElementsByTagName('p');
foreach($pTags as $tag) {
$characterCounter += strlen($tag->nodeValue);
if($characterCounter > 120) {
// this is the desired node, so put html code here
$template = $doc->createDocumentFragment();
$template->appendXML($mcustomHTML);
$tag->appendChild($template);
break;
}
}
return $doc->saveHTML();
这应该适合你。它使用临时 DOM 文档将您拥有的 HTML 字符串转换为可用的内容。然后我们将临时文件的内容导入到主文件中。一旦它被导入,我们就可以像任何其他节点一样简单地附加它。
<?php
$mcustomHTML = '<div style="position:relative; overflow:hidden;"><a href="https://example.com/code=123456"><img src="https://s3.amazonaws.com/a.example.com/image.png" alt="No image" /></a></div>';
$customDoc = new DOMDocument();
$customDoc->loadHTML($mcustomHTML, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$doc = new DOMDocument();
$doc->loadHTML($content);
$customImport = $doc->importNode($customDoc->documentElement, true);
// read all <p> tags and count the text until reach character 120
// then add the custom html into current node
$pTags = $doc->getElementsByTagName('p');
foreach($pTags as $tag) {
$characterCounter += strlen($tag->nodeValue);
if($characterCounter > 120) {
// this is the desired node, so put html code here
$tag->appendChild($customImport);
break;
}
}
return $doc->saveHTML();