dongpo7467 2011-02-10 19:51
浏览 75
已采纳

php DOMDocument-> getElementById-> nodeValue sripping html

I am using php's DOMDocument->getElementById->nodeValue to set a particular DOM element's HTML. The problem is that the string is converted to HTML entities: eg: nodeValue = html_entity_decode('<b>test</b>'); should output 'test' but instead it outputs '&lt;b&gt;test&lt;/b&gt;'

Any ideas why? This happens even if i don't use the html_entity_decode function

Here is my updated script...which is NOW working:

// Construct a DOM object for updating the affected node
$html = new DOMDocument("1.0", "utf-8"); 
if (!$html) return FALSE;

// Load the HTML file in question
$loaded = $html->loadHTMLFile($data['page_path']);
if (!$loaded)
{
    print 'Failed to load file';
    return FALSE;
}

// Establish the node being updated within the file
foreach ($data['divids'] as $divid)
{
    $element = $html->getElementById($divid);
    if (is_null($element))
    {
        print 'Failed to get existing element';
        return FALSE;
    }

    $newelement = $html->createElement('div');
    if (is_null($newelement))
    {
        print 'Failed to create new element';
        return FALSE;
    }
    $newelement->setAttribute('id', $divid);
    $newelement->setAttribute('class', 'reusable-block');

    // Perform the replacement
    $newelement->nodeValue = $replacement;
    $parent = $element->parentNode;
    $parent->replaceChild($newelement, $element);

    // Save the file back to its location
    $saved = $html->saveHTMLFile($data['page_path']);
    if (!$saved)
    {
        print 'Failed to save file';
        return FALSE;
    }
}

// Replace HTML entities left over
$content = files::readFile($data['page_path']);
$content = str_replace('&lt;', '<', $content);
$content = str_replace('&gt;', '>', $content);
if (!@fwrite($handle, $content))
{
    print 'Failed to replace entities';
    return FALSE;
}
  • 写回答

1条回答 默认 最新

  • dongren9739 2011-02-10 19:56
    关注

    This is proper behavior - your tag is being converted to a string, and strings in XML can't contain angle brackets (only tags can). Try converting the HTML into a DOMNode and appending it instead:

    $node = $mydoc->createElement("b");
    $node->nodeValue = "test";
    $mydoc->getElementById("whatever")->appendChild($node);
    

    Update with working example:

    $html = '<html>
        <body id="myBody">
            <b id="myBTag">my old element</b>
        </body>
    </html>';
    
    $mydoc = new DOMDocument("1.0", "utf-8");
    $mydoc->loadXML($html);
    
    // need to do this to get getElementById() to work
    $all_tags = $mydoc->documentElement->getElementsByTagName("*");
    foreach ($all_tags as $element) {
        $element->setIdAttribute("id", true);
    }
    
    $current_b_tag = $mydoc->getElementById("myBTag");
    $new_b_tag = $mydoc->createElement("b");
    $new_b_tag->nodeValue = "my new element";
    $result = $mydoc->getElementById("myBody");
    $result->replaceChild($new_b_tag, $current_b_tag);
    
    echo $mydoc->saveXML($mydoc->documentElement);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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