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