libxml_get_errors
(PHP 5 >= 5.1.0, PHP 7)
libxml_get_errors - 检索错误数组
描述
array libxml_get_errors ( void )
检索错误数组。
返回值
如果缓冲区中存在任何错误,则返回包含LibXMLError对象的数组,否则返回空数组。
例子
示例#1 libxml_get_errors()示例
这个例子演示了如何构建一个简单的libxml错误处理程序。
<?php
libxml_use_internal_errors(true);
$xmlstr?=?<<<?XML
<?xml?version='1.0'?standalone='yes'?>
<movies>
?<movie>
??<titles>PHP:?Behind?the?Parser</title>
?</movie>
</movies>
XML;
$doc?=?simplexml_load_string($xmlstr);
$xml?=?explode("\n",?$xmlstr);
if?($doc?===?false)?{
????$errors?=?libxml_get_errors();
????foreach?($errors?as?$error)?{
????????echo?display_xml_error($error,?$xml);
????}
????libxml_clear_errors();
}
function?display_xml_error($error,?$xml)
{
????$return??=?$xml[$error->line?-?1]?.?"\n";
????$return?.=?str_repeat('-',?$error->column)?.?"^\n";
????switch?($error->level)?{
????????case?LIBXML_ERR_WARNING:
????????????$return?.=?"Warning?$error->code:?";
????????????break;
?????????case?LIBXML_ERR_ERROR:
????????????$return?.=?"Error?$error->code:?";
????????????break;
????????case?LIBXML_ERR_FATAL:
????????????$return?.=?"Fatal?Error?$error->code:?";
????????????break;
????}
????$return?.=?trim($error->message)?.
???????????????"\n??Line:?$error->line"?.
???????????????"\n??Column:?$error->column";
????if?($error->file)?{
????????$return?.=?"\n??File:?$error->file";
????}
????return?"$return\n\n--------------------------------------------\n\n";
}
?>
上面的例子将输出:
<titles>PHP: Behind the Parser</title>
----------------------------------------------^
Fatal Error 76: Opening and ending tag mismatch: titles line 4 and title
Line: 4
Column: 46
--------------------------------------------
扩展内容
- libxml_get_last_error() - 从libxml中检索最后一个错误
- libxml_clear_errors() - 清除libxml错误缓冲区
← libxml_disable_entity_loader
libxml_get_last_error →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com