002.phpt 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. --TEST--
  2. libxml_get_errors()
  3. --EXTENSIONS--
  4. simplexml
  5. --FILE--
  6. <?php
  7. var_dump(libxml_use_internal_errors(true));
  8. $xmlstr = <<< XML
  9. <?xml version='1.0' standalone='yes'?>
  10. <movies>
  11. <movie>
  12. <titles>PHP: Behind the Parser</title>
  13. </movie>
  14. </movies>
  15. XML;
  16. $doc = simplexml_load_string($xmlstr);
  17. $xml = explode("\n", $xmlstr);
  18. if (!$doc) {
  19. $errors = libxml_get_errors();
  20. foreach ($errors as $error) {
  21. echo display_xml_error($error, $xml);
  22. }
  23. var_dump(libxml_get_last_error());
  24. }
  25. function display_xml_error($error, $xml)
  26. {
  27. $return = $xml[$error->line - 1] . "\n";
  28. $return .= str_repeat('-', $error->column) . "^\n";
  29. switch ($error->level) {
  30. case LIBXML_ERR_WARNING:
  31. $return .= "Warning $error->code: ";
  32. break;
  33. case LIBXML_ERR_ERROR:
  34. $return .= "Error $error->code: ";
  35. break;
  36. case LIBXML_ERR_FATAL:
  37. $return .= "Fatal Error $error->code: ";
  38. break;
  39. }
  40. $return .= trim($error->message) . "\n Line: $error->line" . "\n Column: $error->column";
  41. if ($error->file) {
  42. $return .= "\n File: $error->file";
  43. }
  44. return "$return\n\n--------------------------------------------\n\n";
  45. }
  46. echo "Done\n";
  47. ?>
  48. --EXPECTF--
  49. bool(false)
  50. <titles>PHP: Behind the Parser</title>
  51. %s
  52. Fatal Error 76: Opening and ending tag mismatch: titles line 4 and title
  53. Line: 4
  54. Column: %d
  55. --------------------------------------------
  56. object(LibXMLError)#%d (6) {
  57. ["level"]=>
  58. int(3)
  59. ["code"]=>
  60. int(76)
  61. ["column"]=>
  62. int(%d)
  63. ["message"]=>
  64. string(57) "Opening and ending tag mismatch: titles line 4 and title
  65. "
  66. ["file"]=>
  67. string(0) ""
  68. ["line"]=>
  69. int(4)
  70. }
  71. Done