Guest User

Untitled

a guest
May 17th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. $doc = new DOMDocument('1.0', 'iso-8859-1');
  2. $doc->load($xmlPath);
  3.  
  4. $myXMLString = file_get_contents($xmlPath);
  5. $myXMLString = utf8_decode($myXMLString);
  6. $doc = new DOMDocument('1.0', 'iso-8859-1');
  7. $doc->loadXML($myXMLString);
  8.  
  9. <?php
  10. // german Umlaut ä in latin1 = 0xE4
  11. $xml = '<foo>'.chr(0xE4).'</foo>';
  12.  
  13. $doc = new DOMDocument;
  14. $b = $doc->loadxml($xml);
  15. echo 'with doc->recover=false(default) : ', ($b) ? 'success':'failed', "n";
  16.  
  17. $doc = new DOMDocument;
  18. $doc->recover = true;
  19. $b = $doc->loadxml($xml);
  20. echo 'with doc->recover=true : ', ($b) ? 'success':'failed', "n";
  21.  
  22. Warning: DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding !
  23. Bytes: 0xE4 0x3C 0x2F 0x66 in Entity, line: 1 in test.php on line 6
  24. with doc->recover=false(default) : failed
  25.  
  26. Warning: DOMDocument::loadXML(): Input is not proper UTF-8, indicate encoding !
  27. Bytes: 0xE4 0x3C 0x2F 0x66 in Entity, line: 1 in test.php on line 11
  28. with doc->recover=true : success
  29.  
  30. <?php
  31. $xml = sprintf('<foo>
  32. <ae>%s</ae>
  33. <oe>%s</oe>
  34. &
  35. </foo>', chr(0xE4),chr(0xF6));
  36.  
  37. libxml_use_internal_errors(true);
  38. $doc = new DOMDocument;
  39. $doc->recover = true;
  40. libxml_clear_errors();
  41. $b = $doc->loadxml($xml);
  42. $invalidCharFound = false;
  43. foreach(libxml_get_errors() as $error) {
  44. if ( 9==$error->code && !$invalidCharFound ) {
  45. $invalidCharFound = true;
  46. echo "found invalid char, possibly harmlessn";
  47. }
  48. else {
  49. echo "hm, that's probably more severe: ", $error->message, "n";
  50. }
  51. }
Add Comment
Please, Sign In to add comment