如何在不使用Masterminds / html5-php添加隐式标记的情况下保存HTML

时间:2017-06-23 08:55:33

标签: php html5 dom dom-manipulation

我正在尝试使用masterminds / html5-php来操作一些html5文档。我不知道文档是完整的HTML页面还是HTML代码的某些部分,所以我试图像这样操纵它。

        $html5 = new HTML5();
        $dom = $html5->loadHTML($html);

        if($html5->hasErrors())
            return $html;


        $domObject = new Zend_Dom_Query();
        $domObject->setDocument($dom);

        if (empty($selector))
            return $html;


        $domElements = $domObject->query($selector);
        if ($domElements->count() > 0) {
            foreach ($domElements as $domElement) {
                $domElement->parentNode->removeChild($domElement);
            }
        }

        $result = $html5->saveHTML($dom);
        return $result;

我的问题是saveHTML()将包装html代码,所以我的最终文档最终到处都是标记。

有没有办法改变这种行为?也许通过覆盖saveHTML方法?任何提示都表示赞赏。

这是原始HTML

<div class="language-currency-wrapper ">
    <div class="language-currency-block">
                            <span class="language">
                                        <img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/English.jpg" alt="English" />
                    <span class="io-language-label hidden-xs">English</span>
                                    </span>
        <i class="icon-down-open-mini"></i>
    </div>
    <div class="language-currency-dropdown">
        <div class="form-language list">
            <div class="label">Your Language:</div>
            <a href="http://domain.gr/products/bmw/f32.html?___store=en&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/English.jpg" alt="English" /><span class="hidden-xs">English</span></a>
            <a href="http://domain.gr/products/bmw/f32.html?___store=de&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/German.jpg" alt="German" /><span class="hidden-xs">German</span></a>
            <a href="http://domain.gr/products/bmw/f32.html?___store=el&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/Greek.jpg" alt="Greek" /><span class="hidden-xs">Greek</span></a>
        </div>
    </div>
    <div class="clearfix"></div>
</div>

以下是saveHTML生成的内容

<!DOCTYPE html>
<html><div class="language-currency-wrapper ">
    <div class="language-currency-dropdown">
        <div class="form-language list">
            <div class="label">Your Language:</div>
            <a href="http://domain.gr/products/bmw/f32.html?___store=en&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/English.jpg" alt="English"><span class="hidden-xs">English</span></a>
            <a href="http://domain.gr/products/bmw/f32.html?___store=de&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/German.jpg" alt="German"><span class="hidden-xs">German</span></a>
            <a href="http://domain.gr/products/bmw/f32.html?___store=el&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/Greek.jpg" alt="Greek"><span class="hidden-xs">Greek</span></a>
        </div>
    </div>
    <div class="clearfix"></div>
</div>
</html>

这是我所期待的

<div class="language-currency-wrapper ">
    <div class="language-currency-dropdown">
        <div class="form-language list">
            <div class="label">Your Language:</div>
            <a href="http://domain.gr/products/bmw/f32.html?___store=en&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/English.jpg" alt="English"><span class="hidden-xs">English</span></a>
            <a href="http://domain.gr/products/bmw/f32.html?___store=de&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/German.jpg" alt="German"><span class="hidden-xs">German</span></a>
            <a href="http://domain.gr/products/bmw/f32.html?___store=el&amp;___from_store=en"><img src="http://domain.gr/skin/frontend/be/ioweb/images/lang/Greek.jpg" alt="Greek"><span class="hidden-xs">Greek</span></a>
        </div>
    </div>
    <div class="clearfix"></div>
</div>

为简单起见,我删除了语言块

0 个答案:

没有答案