PHP LIBXML_NOWARNING不禁止显示警告吗? [英] PHP LIBXML_NOWARNING not suppressing warnings?
问题描述
在使用PHPDOMDocument-> loadHTML加载html时,使用LIBXML_NOWARNING选项标志不会停止使用.其他常数确实起作用.
在下面的示例中,我添加了LIBXML_HTML_NODEFDTD来证明已接收到常量(阻止添加doctype).
$doc=new DOMDocument();
$doc->loadHTML("<tagthatdoesnotexist><h1>Hi</h1></tagthatdoesnotexist>",LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_HTML_NODEFDTD);
echo $doc->saveHTML();
但是,仍然会生成警告并输出警告.我想念什么?
< DOMDocument::loadHTML()
忽略了c0>选项,这是PHP中的一个缺陷(并且固定的).它最近在相关问题"libxml htmlParseDocument忽略htmlParseOption标志" 中提出,并提起作为 PHP错误#74004 LIBXML_NOWARNING
标志在loadHTML * 上被忽略.>
但是,您可以自行处理错误,直到缺陷消除为止
- 在调用
libxml_use_internal_errors(true)
. loadHTML"rel =" nofollow noreferrer>DOMDocument::loadHTML
.这将防止错误冒泡到您的默认错误处理程序.然后,您可以使用其他libxml错误函数(例如,libxml_get_errors()
)来获取它们(如果需要). - 使用此功能时,请确保清除内部错误缓冲区.如果您不这样做,并且正在长期运行中使用它,则可能会发现您的所有内存都用光了.
- 如果要恢复默认功能集
libxml_use_internal_errors()
.
代码示例:
$doc = new DOMDocument();
# clear errors list if any
libxml_clear_errors();
# use internal errors, don't spill out warnings
$previous = libxml_use_internal_errors(true);
$doc->loadHTML("<tagthatdoesnotexist><h1>Hi</h1></tagthatdoesnotexist>");
echo $doc->saveHTML();
# clear errors list if any
libxml_clear_errors();
# restore previous behavior
libxml_use_internal_errors($previous);
更新
Using the LIBXML_NOWARNING options flag doesn't stop wanrings when loading html with PHPDOMDocument->loadHTML. Other constants do work though.
In the example below I add the LIBXML_HTML_NODEFDTD to prove that the constants are received(stops a doctype from being added).
$doc=new DOMDocument();
$doc->loadHTML("<tagthatdoesnotexist><h1>Hi</h1></tagthatdoesnotexist>",LIBXML_NOERROR | LIBXML_NOWARNING | LIBXML_HTML_NODEFDTD);
echo $doc->saveHTML();
However, warnings are still generated and output. What am I missing?
That the LIBXML_NOWARNING
option is ignored with DOMDocument::loadHTML()
is a flaw in PHP (and to be fixed). It has been recently brought up in a related question "libxml htmlParseDocument ignoring htmlParseOption flags" and filed as PHP Bug #74004 LIBXML_NOWARNING
flag ingnored on loadHTML*.
You can, however, manage the error handling your own until the flaw is removed:
- Set
libxml_use_internal_errors(true)
before callingDOMDocument::loadHTML
. This will prevent errors from bubbling up to your default error handler. And you can then get at them (if you desire) using other libxml error functions (e.g.libxml_get_errors()
). - When using this function, be sure to clear your internal error buffer. If you don't and you are using this in a long running process, you may find that all your memory is used up.
- If you want to restore the the default functionality set
libxml_use_internal_errors()
.
Code example:
$doc = new DOMDocument();
# clear errors list if any
libxml_clear_errors();
# use internal errors, don't spill out warnings
$previous = libxml_use_internal_errors(true);
$doc->loadHTML("<tagthatdoesnotexist><h1>Hi</h1></tagthatdoesnotexist>");
echo $doc->saveHTML();
# clear errors list if any
libxml_clear_errors();
# restore previous behavior
libxml_use_internal_errors($previous);
Update
这篇关于PHP LIBXML_NOWARNING不禁止显示警告吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!