[英]printing out html content from domelement using nodeValue
I have image in html.我在 html 中有图像。 I parse it to DOMDocument and start working with it...我将其解析为 DOMDocument 并开始使用它...
$doc = new DOMDocument();
$doc->loadHTML($article_header);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
$container = $img->parentNode;
if ($container->tagName != "a") {
$image_inside=utf8_decode($img->nodeValue);
echo "3".$image_inside;
die;
}
}
This code works fine line 3 gets image.此代码工作正常第 3 行获取图像。 line 6 understands that there is no "a" tag above this "img" tag, and line 8 must print out my initial image.第 6 行知道这个“img”标签上方没有“a”标签,第 8 行必须打印出我的初始图像。 But the thing is I only see "3" without image tag and etc...但问题是我只看到没有图像标签的“3”等等......
I did inspect element and nothing is there.我确实检查了元素,但那里什么也没有。 just "3" is coming out.只是“3”出来了。 Why I cannot print out image?为什么我无法打印出图像?
You could use:你可以使用:
DOMDocument::saveXML($img);
From PHP Documetation's saveXML() .来自 PHP 文档的saveXML() 。
$doc = new DOMDocument();
$doc->loadHTML($article_header);
$imgs = $doc->getElementsByTagName('img');
foreach ($imgs as $img) {
$container = $img->parentNode;
if ($container->tagName != "a") {
echo utf8_decode($doc->saveXML($img));
die;
}
}
If you're using PHP 5.3.6 you could use (from How to return outer html of DOMDocument? )如果您使用的是 PHP 5.3.6,您可以使用(来自如何返回 DOMDocument 的外部 html? )
$doc->saveHtml($img);
Note the caveat mentioned in the linked-to question:请注意链接到问题中提到的警告:
(...) use saveXml(), but that would create XML compliant markup. (...) 使用 saveXml(),但这会创建符合 XML 的标记。 In the case of an
<a>(<img>)
element, that shouldn't be an issue though.对于<a>(<img>)
元素,这应该不是问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.