Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用DOMDocument替换div的图像标记并返回修改后的html_Php_Domdocument - Fatal编程技术网

Php 用DOMDocument替换div的图像标记并返回修改后的html

Php 用DOMDocument替换div的图像标记并返回修改后的html,php,domdocument,Php,Domdocument,在下面的代码中,我试图用一些html替换$content中的每个图像。使用$dom->saveHTML($image)我得到了图像html,但是str\u replace不会替换它。我不知道为什么 $content=''; $dom=新的DOMDocument; $dom->loadHTML($content); $images=$dom->getElementsByTagName('img'); foreach($images作为$image){ $i=$dom->saveHTML($imag

在下面的代码中,我试图用一些html替换
$content
中的每个图像。使用
$dom->saveHTML($image)
我得到了图像html,但是
str\u replace
不会替换它。我不知道为什么

$content='';
$dom=新的DOMDocument;
$dom->loadHTML($content);
$images=$dom->getElementsByTagName('img');
foreach($images作为$image){
$i=$dom->saveHTML($image);
$replacement='test';
$content=str_replace($i,$replacement,$content);
}
//$content应与以前的编辑一起更改。
返回$content;
最后,
$content
应返回:

测试

有什么想法吗?

替换DOMNode的方法是使用方法
replaceChild
stru replace
只处理字符串):

如果替换比包含文本的div标记更复杂,则只需以相同的方式构建替换节点(将其他节点附加到它)。要处理属性,请参见

$dom=newdomdocument;
$dom->loadHTML($content);
$images=$dom->getElementsByTagName('img');
foreach($images作为$image){
$node=$dom->createElement(“div”);
$frag=$dom->createDocumentFragment();//创建片段
$frag->appendXML('test');
$node.appendChild($frag);
$image->parentNode->replaceChild($node,$image);
}
它是HTML。HTML的动态特性使得正确执行正则表达式或字符串替换非常困难。然而,PHP允许通过
DomDocument
进行dom操作。因此,选择imagesparentnode并用div替换图像


使用
createDocumentFragment()
对其进行了更新。这将在内存中创建一个DOM片段。您可以将XML(读取HTML)作为字符串附加到它。然后可以将片段附加到节点。

不要使用str\u replace。这是一个DOM,我们可以使用replaceChild。@InwPetrust:我看了一下,现在所有元素都可以自己完成了(请参见我如何将文本节点添加到div节点)。谢谢!我对我的问题进行了编辑,以反映我首先需要的内容,您能否就如何将$content作为修改过的var返回给我一些建议?@inwpitrust。按要求。然而,你应该从一开始就在你的问题中明确这一点。我们在这里的回答为您提出的问题提供了解决方案。发布新问题或在原始问题中显示最终结果。“在原始问题中显示最终结果。”你的意思是我应该在我的问题中添加你的回答中给出的元素?这不是很重要吗?谢谢你的帮助!我的意思是,你已经分三步构建了你的问题。首先描述一下你到底需要什么。好的,我已经用你的建议编辑了我的问题。您知道如何使用以前所做的编辑更改$content吗?
$dom = new DOMDocument;
$dom->loadHTML($content);
$imgNodeList = $dom->getElementsByTagName('img');

foreach ($imgNodeList as $imgNode) {
    $divNode = $dom->createElement('div');
    $textNode = $dom->createTextNode('test');
    $divNode->appendChild($textNode);

    $imgNode->parentNode->replaceChild($divNode, $imgNode);
 }
$dom = new DOMDocument;
$dom->loadHTML($content);
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
    $node = $dom->createElement("div");
    $frag = $dom->createDocumentFragment(); // create fragment
    $frag->appendXML('<div class="test"><a href="#">some link</a><span>test</span></div>');
    $node.appendChild($frag);
    $image->parentNode->replaceChild($node, $image);
}