PHP LIBXML

编程入门 行业动态 更新时间:2024-10-11 07:28:57
本文介绍了PHP LIBXML_NOWARNING不禁止显示警告吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在使用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 calling DOMDocument::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

    This bug is fixed now.

    更多推荐

    PHP LIBXML

    本文发布于:2023-11-09 07:43:08,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1571767.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:PHP   LIBXML

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!