duanqin9631 2015-02-19 21:54
浏览 44
已采纳

DOMDocument使用额外的标签保存html

I am using HTMLDom to manipulate a string, rather than a complete webpage. When I use saveHTML() it automatically throws in doctype and html tags.

$str = 'frament containing html';
$str = utf8_encode($str);
$doc->LoadHTML($str);
...do stuff...
$str = $doc->saveHTML();

What is the correct way to save a fragment of HTML without the automatic inclusion of extra tags. Failing that; the correct method to remove these extra tags?

I used an html parser to avoid using regex's, so it seems a little counter-intuitive to have to use them on the output of a parser.

  • 写回答

1条回答 默认 最新

  • doushi3322 2015-02-20 10:28
    关注

    PHPs DOMDocument repairs the document if you load HTML. That means it adds the html and body elements.

    So you need to fetch all nodes inside body and save them as HTML.

    $html = <<<'HTML'
    <h1>Hello World</h1>
    Text
    <!-- comment -->
    HTML;
    
    $dom = new DOMDocument();
    $dom->loadHtml($html);
    $xpath = new DOMXPath($dom);
    
    $result = '';
    foreach ($xpath->evaluate('/html/body/node()') as $node) {
      $result .= $dom->saveHtml($node);
    }
    
    echo $result;
    

    Here is another option, but it is not available everywhere yet. PHP added LIBXML_HTML_NOIMPLIED and LIBXML_HTML_NODEFDTD options.

    $dom->loadHtml($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    

    PHP <= 5.3

    The first an best option would be to update the PHP. PHP 5.3 is no longer maintained.

    The second option is using DOMDocument::saveXML($node, LIBXML_NOEMPTYTAG). This will generate an XML (XHTML) fragment, but should be enough for the most cases.

    The last option would be using the string functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?