appendXML剥离img元素 我需要在文章的中间插入一个带有div元素的图像。该页面是使用PHP从CRM生成的。我有一个例程来计算所有段落标记的字符数,并在包含第120个字符的段落后插入HTML。我使用的是appendXML,它可以工作,直到我尝试插入一个图像元素

appendXML剥离img元素 我需要在文章的中间插入一个带有div元素的图像。该页面是使用PHP从CRM生成的。我有一个例程来计算所有段落标记的字符数,并在包含第120个字符的段落后插入HTML。我使用的是appendXML,它可以工作,直到我尝试插入一个图像元素,php,domdocument,Php,Domdocument,当我将元素放入时,它会被剥离。我知道它在寻找XML,但是,我正在关闭标记,我知道这会有所帮助 有没有一种方法可以使用appendXML而不删除img元素 $mcustomHTML=”“; $doc=新的DOMDocument(); $doc->loadHTML(“”.$content); //读取所有标记,并计数文本,直到达到字符120 //然后将自定义html添加到当前节点中 $pTags=$doc->getElementsByTagName('p'); foreach($ptag作为$tag

当我将
元素放入时,它会被剥离。我知道它在寻找XML,但是,我正在关闭
标记,我知道这会有所帮助

有没有一种方法可以使用
appendXML
而不删除img元素

$mcustomHTML=”“;
$doc=新的DOMDocument();
$doc->loadHTML(“”.$content);
//读取所有标记,并计数文本,直到达到字符120
//然后将自定义html添加到当前节点中
$pTags=$doc->getElementsByTagName('p');
foreach($ptag作为$tag){
$characterCounter+=strlen($tag->nodeValue);
如果($characterCounter>120){
//这是所需的节点,因此将html代码放在这里
$template=$doc->createDocumentFragment();
$template->appendXML($mcustomHTML);
$tag->appendChild($template);
打破
}
}
返回$doc->saveHTML();

这应该适合您。它使用一个临时DOM文档将您拥有的HTML字符串转换为可操作的内容。然后我们将临时文档的内容导入到主文档中。导入后,我们可以像其他节点一样简单地附加它

$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();

可能需要使用Cdata、标签。这家伙
我不知道DomDocument是否有方法,通常我使用XMLWriter for XML,它有更多关于CDATA的内容
CDATA代表字符数据,这意味着这些字符串之间的数据包括可以解释为XML标记的数据,但是不应该这样。
换句话说,将HTML放入其中,XML解析器将忽略它。正如我也提到的,我可能不会选择DomDocument作为编写XML的第一选择。(就我个人而言,我从未有过使用Dom的良好经验)您没有使用XML。您可以使用
loadHTML()
saveHTML()
$content
中有什么内容?为什么在它前面放一个XML声明?$content是将发布到站点的HTML文章。它是在fly上创建的有人建议使用xml命令,不确定为什么工作得很好,谢谢!
<?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();