douqiaolong0528 2013-05-27 16:09
浏览 15
已采纳

当<html>,<head>,<body>标记未设置时,将PHP DOM设置为不正确的html

He I have made this function to set attributes on 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));
    }
}

When I don't use this to edit the entire document but just a snap of it like:

<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>

it assumes that the rest of the document is missing and it adds the additional html tags like this:

<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>

It does not hurt directly but it looks ugly. The reason I use it this way is because I use ajax a lot and I only send back a snippet of the html not a whole document.

So anyone knows how to make DOM stop doing this? Or is this impossible. I can also just use str_replace to delete them when I use my function but I consider that a ugly fix.

  • 写回答

2条回答 默认 最新

  • douhuai2861 2013-05-27 17:43
    关注

    Adding the <meta> tag will trigger the fixing behavior of DOMDocument. The good part is that you don't need to add that tag at all. If you wan't to use an encoding of your choosing just pass it as a constructor argument.

    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();
    

    Output

    <div>Hello World</div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 使用Java milo连接Kepserver服务端报错?
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了