
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP - xml_set_unparsed_entity_decl_handler() Function
The PHP XML Parser xml_set_unparsed_entity_decl_handler() function is used to specify a handler function for unparsed entity declarations in an XML parser. This handler is triggered when the parser detects an unparsed entity in the XML data. It allows developers to handle or respond to such situations during XML processing. The function's inputs should contain the handler function's name and an XML parser resource. It is a function of the XML Parser extension for PHP that must be enabled.
Syntax
Below is the syntax of the PHP XML Parser xml_set_unparsed_entity_decl_handler() function −
true xml_set_unparsed_entity_decl_handler(XMLParser $xml_parser, callable|string|null $handler)
Parameters
Here are the parameters of the xml_set_unparsed_entity_decl_handler() function −
$xml_parser: (Required) It is a reference to the XML parser to use inside the object.
$handler: (Required) It is used to specifies name of function that must exist when xml_parse().
Handler Function Signature
The signature of the handler is as follows −
void handler( XMLParser $xml_parser, string $entity_name, string|false $base, string $system_id, string|false $public_id, string|false $notation_name )
Parameters
Below are the parameters of the handler function −
$xml_parser: Specify a variable containing the XML parser calling the handler.
$entity_name: The name of the entity that is about to be defined.
$base: This is a starting point for finding out the external entity's system identification (systemId).
$system_id: It is used to identifier the external entity.
$public_id: It is used to public identifier the external entity.
$notation_name: It is the name of the notation of this entity.
Return Value
The xml_set_unparsed_entity_decl_handler() function returns TRUE on success. And FALSE on failure.
PHP Version
First introduced in core PHP 4, the xml_set_unparsed_entity_decl_handler() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
Here is the basic example of the PHP XML Parser xml_set_unparsed_entity_decl_handler() function to set a callback handler (unparsedEntityHandler) in the event that an unparsed entity declaration is present in the XML data. The handler method will be called in order to handle any unparsed entity declarations that are found during XML parsing.
<?php // Define the handler function function unparsedEntityHandler($parser, $entity_name, $base, $system_id, $public_id, $notation_name) { echo "Unparsed entity: $entity_name, System ID: $system_id, Notation Name: $notation_name\n"; } // Create an XML parser $parser = xml_parser_create(); // Set the unparsed entity declaration handler xml_set_unparsed_entity_decl_handler($parser, 'unparsedEntityHandler'); // Define an XML string $xml_data = <<<XML <!DOCTYPE example [ <!ELEMENT example EMPTY> <!ENTITY myImage SYSTEM "image.jpg" NDATA jpeg> ]> <example/> XML; // Parse the XML data if (!xml_parse($parser, $xml_data)) { echo "Error: ", xml_error_string(xml_get_error_code($parser)), "\n"; } // Free the parser xml_parser_free($parser); ?>
Output
Here is the outcome of the following code −
Unparsed entity: myImage, System ID: image.jpg, Notation Name: jpeg
Example 2
In the below PHP code we will parse and handle multiple entities using the xml_set_unparsed_entity_decl_handler() function and show how to parse an XML document, highlighting the handler's handling of each of the unparsed entities.
<?php // Define the handler function function unparsedEntityHandler($parser, $entity_name, $base, $system_id, $public_id, $notation_name) { echo "Entity Name: $entity_name\nSystem ID: $system_id\nNotation: $notation_name\n---\n"; } // Create an XML parser $parser = xml_parser_create(); // Set the unparsed entity declaration handler xml_set_unparsed_entity_decl_handler($parser, 'unparsedEntityHandler'); // Parse XML with multiple unparsed entities $xml_data = <<<XML <!DOCTYPE example [ <!ENTITY entity1 SYSTEM "entity1.data" NDATA notation1> <!ENTITY entity2 SYSTEM "entity2.data" NDATA notation2> <!NOTATION notation1 SYSTEM "notation1.def"> <!NOTATION notation2 SYSTEM "notation2.def"> ]> <example>&entity1;&entity2;</example> XML; xml_parse($parser, $xml_data); // Free the parser xml_parser_free($parser); ?>
Output
This will generate the below output −
Entity Name: entity1 System ID: entity1.data Notation: notation1 --- Entity Name: entity2 System ID: entity2.data Notation: notation2 ---
Example 3
Now the below code uses the xml_set_unparsed_entity_decl_handler function. And we will un-parse entities are handled dynamically, and their data is saved in an associative array for future use.
<?php // Define an array $unparsedEntities = []; // Define the handler function function unparsedEntityHandler($parser, $entity_name, $base, $system_id, $public_id, $notation_name) { global $unparsedEntities; $unparsedEntities[$entity_name] = [ 'base' => $base, 'system_id' => $system_id, 'public_id' => $public_id, 'notation_name' => $notation_name ]; } // Create an XML parser $parser = xml_parser_create(); // Set the unparsed entity declaration handler xml_set_unparsed_entity_decl_handler($parser, 'unparsedEntityHandler'); // Parse a complex XML document $xml_data = <<<XML <!DOCTYPE example [ <!ENTITY video SYSTEM "video.mp4" NDATA video_format> <!ENTITY audio SYSTEM "audio.mp3" NDATA audio_format> <!NOTATION video_format SYSTEM "video_codec"> <!NOTATION audio_format SYSTEM "audio_codec"> ]> <media> <file>&video;</file> <file>&audio;</file> </media> XML; xml_parse($parser, $xml_data); // Display the stored entity details print_r($unparsedEntities); // Free the parser xml_parser_free($parser); ?>
Output
This will create the below output −
Array ( [video] => Array ( [base] => [system_id] => video.mp4 [public_id] => [notation_name] => video_format ) [audio] => Array ( [base] => [system_id] => audio.mp3 [public_id] => [notation_name] => audio_format ) )