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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里