PHP - libxml_set_external_entity_loader() Function
Definition and Usage
XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The libXMLError class contains the errors thrown by the libxml library.
The libxml_set_external_entity_loader() function is used to change the default external entity loader.
Syntax
libxml_set_external_entity_loader($function);
Parameters
| Sr.No | Parameter & Description |
|---|---|
| 1 |
function(Mandatory) This is a callable function with 3 arguments namely, public id, system id, and a context. |
Return Values
This function returns a boolean value which is TRUE incase of success and FALSE incase of a failure.
PHP Version
This function was first introduced in PHP Version 5 and works in all the later versions.
Example
Following example demonstrates the usage of the libxml_set_external_entity_loader() function −
<html>
<head>
<body>
<?php
$str = <<<XML
<!DOCTYPE test PUBLIC "-//TEST/BAR" "http://test.com/testbar">
<test>bar</test>
$dtd = <<<DTD
<!ELEMENT test (#PCDATA)>
libxml_set_external_entity_loader(
function ($public, $sys, $context) use($dtd) {
print_r($public);
print_r($sys);
print_r($context);
$f = fopen("php://temp", "r+");
fwrite($f, $dtd);
rewind($f);
return $f;
}
);
$doc = new DOMDocument;
$r = $doc->loadXML($str);
print_r($doc->validate());
?>
</body>
</head>
</html>
This will produce following result −
-//TEST/BARhttp://test.com/testbarArray ( [directory] => [intSubName] => [extSubURI] => [extSubSystem] => ) 1
php_function_reference.htm
Advertisements