简体   繁体   English

使用PHP DomDocument时如何排除警告消息?

[英]How can I exclude warning messages when using PHP DomDocument?

I'm trying to use the DomDocument class to load and analyse an HTML fragment (doesn't include the <html> and <body> tags). 我正在尝试使用DomDocument类加载和分析HTML片段(不包括<html><body>标记)。 There is a lot of garbage left over from MS-Word when it was converted into HTML, so I'm getting warning messages such as DOMDocument::loadHTML(): Tag o:p invalid in Entity, line: 69 ddtest.d8.drush.inc:68 . 当MS-Word转换为HTML时,有很多垃圾,因此我收到诸如DOMDocument::loadHTML(): Tag o:p invalid in Entity, line: 69 ddtest.d8.drush.inc:68警告消息DOMDocument::loadHTML(): Tag o:p invalid in Entity, line: 69 ddtest.d8.drush.inc:68 Here is the relevant code: 以下是相关代码:

    $dom = new DOMDocument;
    //load the html into the object
    $dom->loadHTML($row->body_value);

I've tried to get rid of the warning messages by using this: 我尝试使用以下方法消除警告消息:

    $dom = new DOMDocument;

    //load the html into the object
    $dom->loadHTML($row->body_value, LIBXML_NOWARNING);

But it has no effect, the warning messages are still displayed. 但是它没有任何作用,仍然显示警告消息。 What am I doing wrong? 我究竟做错了什么?

You could try using the error handling of libxml like this perhaps: 您可以尝试使用libxml的错误处理,如下所示:

libxml_use_internal_errors( true );

$dom=new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = false;
$dom->standalone=true;
$dom->strictErrorChecking=false;
$dom->substituteEntities=true;
$dom->recover=true;
$dom->formatOutput=false;
$dom->loadHTML( $row->body_value );

libxml_clear_errors();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM