dtbam62840 2017-06-21 03:28
浏览 33
已采纳

codeigniter,查看模板字符串到DomDocument操作

I have a php file in application\views\article.php

article.php content:

<!DOCTYPE html>
<html prefix='og: http://ogp.me/ns#'>
<head>
    <title>test</title>
</head>
<body>
<div> test div1 </div>
<div> test div2 </div>
</body>
</html>

When I use $this->load->view() to load article.php template and use DomDocument to get dom.

$html=$this->load->view('article','',TRUE);
$doc = new DomDocument;
$doc->loadHTML($html);      
echo $doc->saveXML($doc->getElementsByTagName('div')->item(0));
// or echo $doc->saveXML();

have the error message: Message: DOMDocument::loadHTML(): Unexpected end tag : meta in Entity, line: 4

but whe I use this:

$html='<!DOCTYPE html>
    <html prefix=\'og: http://ogp.me/ns#\'>
    <head>
        <title>test</title>
    </head>
    <body>
    <div>   test div1 </div>
    <div> test div2 </div>
    <p>Directory </p>
    </body>
    </html>';

  $doc->loadHTML($html);         
  echo $doc->saveXML($doc->getElementsByTagName('div')->item(0));
  // or echo $doc->saveXML();

this is success.

gettype($html) to two methods of $html, both are strings.

  • 写回答

1条回答 默认 最新

  • duanlin6989 2017-06-21 04:12
    关注

    Try hide the warning with

    libxml_use_internal_errors(true);
    

    Or:

    @$doc->loadHTML($html);
    

    The warning is because the HTML returned by $this->load->view('article','',TRUE); is invalid, loadHTML() resolve this but show the warnings.

    Manual

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

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程