在使用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 * 上被忽略.
但是,您可以自行处理错误,直到缺陷消除为止
代码示例:
$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:
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
This bug is fixed now.
更多推荐
PHP LIBXML
发布评论