【问题标题】:Set PHP DOM to not correct html when <html>,<head>,<body> tags are not set未设置 <html>、<head>、<body> 标签时,将 PHP DOM 设置为不正确的 html
【发布时间】:2013-05-22 12:47:53
【问题描述】:

我已经做了这个函数来设置id的属性:

function set_atr($id, $attribute, $value, $source){

    $dom = new DomDocument();

    $dom->loadHTML('<meta http-equiv="content-type" content="text/html; charset=utf-8">'.$source);

    $xp = new DOMXPath($dom);
    $element = $xp->query('//*[@id="'.$id.'"]')->item(0);

    if(!$element){
        return false;
    }
    else{
        $element->setAttribute($attribute, $value);
        return str_replace('<meta http-equiv="content-type" content="text/html; charset=utf-8">', '', $dom->saveHTML($dom->documentElement));
    }
}

当我不使用它来编辑整个文档时,只是像这样:

<table>
    <tr>
            <td><h4>login</h4></td>
            <td></td>
    </tr>
    <tr>
        <td>username:</td>
        <td>password:</td>
    </tr>
    <tr>
        <td><input id="username" type="text" placeholder="pipo" name="username" class="form" /></td>
        <td><input id="password" type="password" placeholder="wachtwoord" name="password" class="form" /></td>
    </tr>
    <tr>
        <td id="error1"></td>
        <td id="error2"></td>
    </tr>
    <tr>
        <td><input type="button" value="Login" class="btn" action="url" level="cms_user_login" form="login" /></td>
        <td></td>
    </tr>
</table>

它假定文档的其余部分丢失,并添加额外的 html 标签,如下所示:

<html>
<head></head>
<body>    <table>
    <tr>
            <td><h4>login</h4></td>
            <td></td>
    </tr>
    <tr>
        <td>username:</td>
        <td>password:</td>
    </tr>
    <tr>
        <td><input id="username" type="text" placeholder="pipo" name="username" class="form" /></td>
        <td><input id="password" type="password" placeholder="wachtwoord" name="password" class="form" /></td>
    </tr>
    <tr>
        <td id="error1"></td>
        <td id="error2"></td>
    </tr>
    <tr>
        <td><input type="button" value="Login" class="btn" action="url" level="cms_user_login" form="login" /></td>
        <td></td>
    </tr>
</table></body>
</html>

它不会直接受伤,但看起来很难看。我这样使用它的原因是因为我经常使用 ajax,而且我只发回 html 的 sn-p 而不是整个文档。

所以有人知道如何让 DOM 停止这样做吗?或者这是不可能的。当我使用我的函数时,我也可以只使用 str_replace 删除它们,但我认为这是一个丑陋的修复。

【问题讨论】:

标签: php html dom


【解决方案1】:

添加&lt;meta&gt; 标签将触发DOMDocument 的修复行为。好的部分是您根本不需要添加该标签。如果您不想使用您选择的编码,只需将其作为构造函数参数传递即可。

http://php.net/manual/en/domdocument.construct.php

$doc = new DOMDocument('1.0', 'UTF-8');
$node = $doc->createElement('div', 'Hello World');
$doc->appendChild($node);
echo $doc->saveHTML();

输出

<div>Hello World</div>

【讨论】:

    【解决方案2】:

    如果您使用的是 Libxml >= 2.7.7(自 PHP >= 5.4.0 起),请调用 loadHTML,并将 LIBXML_HTML_NOIMPLIED 作为第二个参数。例如

    $dom->loadHTML($source, LIBXML_HTML_NOIMPLIED);
    

    这将禁止添加隐含的&lt;html&gt;&lt;body&gt; 元素。

    另请参阅this 答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 2016-10-28
      • 2020-09-16
      • 1970-01-01
      相关资源
      最近更新 更多