loadXML - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of loadXML.php

  1. <?php
  2. /**
  3.  * @param string $xmlContent A well-formed XML string
  4.  * @param string $version 1.0
  5.  * @param string $encoding utf-8
  6.  * @return bool
  7.  */
  8. function isXMLContentValid($xmlContent, $version = '1.0', $encoding = 'utf-8'){
  9.     if (trim($xmlContent) == '') {
  10.         return false;
  11.     }
  12.     libxml_use_internal_errors(true);
  13.     $doc = new DOMDocument($version, $encoding);
  14.     $doc->loadXML($xmlContent);
  15.     $errors = libxml_get_errors();
  16.     libxml_clear_errors();
  17.     return empty($errors);
  18. }
  19.  
  20. $xml = '<website><domain>wtools.io</domain><title>Online Web Tools</title></website>';
  21. if(isXMLContentValid($xml)){
  22.     echo "Valid XML string";
  23. } else {
  24.     echo "Not valid JSON string";
  25. }
  26.  
File Description
  • loadXML
  • PHP Code
  • 18 Dec-2020
  • 691 Bytes
You can Share it: