我有以下问题。当超文本标记语言从<img>标签开始并保存$dom->saveHTML()时,我只能得到第一张图片作为响应。但是,当我在HTML之前添加任何字符串时,我会得到额外的<img>标记。为什么会这样呢?
$h = '<img src="https://example.com/one.jpg" alt=""><br><p>bla</p><img src="https://example.com/foo.jpg" alt=""><br>';
$h = 'abc<img src="https://example.com/one.jpg" alt=""><br><p>bla</p><img src="https://example.com/foo.jpg" alt=""><br>';以上是示例输入
<?php
$h = '<img src="https://example.com/one.jpg" alt=""><br><p>bla</p><img src="https://example.com/foo.jpg" alt=""><br>';
echo'start<br />';
echo htmlspecialchars($h);
echo'<br />end<br />';
$dom = new domDocument();
$dom->loadHTML($h, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$img_class = $image->getAttribute('class');
if($img_class == '') {
$image->setAttribute('class', 'img-responsive img-rounded');
echo'add class <br />';
}
}
$my_post_content = $dom->saveHTML();
echo'start<br />';
echo htmlspecialchars($my_post_content);
echo'<br />end<br />';发布于 2017-02-24 23:14:44
你好,朋友,我已经对你的脚本做了一些测试,第二个图像似乎因为你传递给$dom->loadHTML($h, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);的LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD而消失了
可以有一个简单的解决方案来实现这个"hack“,并使用如下所示:
$h = 'abc<img src="https://example.com/one.jpg" alt=""><br><p>bla</p><img src="https://example.com/foo.jpg" alt=""><br>';
然后只需手动从字符串中剪切必要的内容,但我给您一个更好的解决方案:
$h = '<img src="https://example.com/one.jpg" alt=""><br><p>bla</p><img src="https://example.com/foo.jpg" alt=""><br>';
echo'start<br />';
echo htmlspecialchars($h);
echo'<br />end<br />';
// blank document is used because we want to extract only the
// html inside <body> from $dom
$blank = new DOMDocument;
// initialize the $dom object and nothing is changed in this code
$dom = new domDocument();
$dom->loadHTML($h);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$img_class = $image->getAttribute('class');
if ($img_class == '') {
$image->setAttribute('class', 'img-responsive img-rounded');
echo'add class <br />';
}
}
// now get the body that will containg updated HTML
// and insert all it's children in the blank document
$body = $dom->getElementsByTagName('body')->item(0);
foreach ($body->childNodes as $child) {
$blank->appendChild($blank->importNode($child, true));
}
$my_post_content = $blank->saveHTML($blank);
echo'start<br />';
echo htmlspecialchars($my_post_content);
echo'<br />end<br />';
exit;输出结果为:
start
<img src="https://example.com/one.jpg" alt=""><br><p>bla</p><img src="https://example.com/foo.jpg" alt=""><br>
end
add class
add class
start
<img src="https://example.com/one.jpg" alt="" class="img-responsive img-rounded"><br><p>bla</p><img src="https://example.com/foo.jpg" alt="" class="img-responsive img-rounded"><br>
end正如你所看到的,你有你们两个的图像。
干杯!
https://stackoverflow.com/questions/42366334
复制相似问题