首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >当domDocument从<img>启动时,php domDocument() saveHTML仅保存第一个图像

当domDocument从<img>启动时,php domDocument() saveHTML仅保存第一个图像
EN

Stack Overflow用户
提问于 2017-02-21 19:41:26
回答 1查看 190关注 0票数 1

我有以下问题。当超文本标记语言从<img>标签开始并保存$dom->saveHTML()时,我只能得到第一张图片作为响应。但是,当我在HTML之前添加任何字符串时,我会得到额外的<img>标记。为什么会这样呢?

代码语言:javascript
复制
$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>';

以上是示例输入

代码语言:javascript
复制
<?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 />';
EN

Stack Overflow用户

回答已采纳

发布于 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>';

然后只需手动从字符串中剪切必要的内容,但我给您一个更好的解决方案:

代码语言:javascript
复制
$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;

输出结果为:

代码语言:javascript
复制
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

正如你所看到的,你有你们两个的图像。

干杯!

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42366334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档