
- 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_parser_set_option() Function
The PHP XML Parser xml_parser_set_option() function is used to set specific XML parser settings. It makes it easier to modify how the parser handles XML data, like changing the text encoding or removing whitespace.
It is necessary to provide the modified value, the change optionand the parser. It returns true if it is successful and false otherwise. As it can result in errors, large documents or invalid options should be handled carefully.
Syntax
Below is the syntax of the PHP XML Parser xml_parser_set_option() function −
bool xml_parser_set_option ( XMLParser $xml_parser, int $option, string|int|bool $value )
Parameters
Here are the parameters of the xml_parser_set_option() function −
$xml_parser: (Required) It is used to specify the XML parser to use.
$option: (Required) It is used to specify the option to set. Possible values are - XML_OPTION_CASE_FOLDING, XML_OPTION_SKIP_TAGSTART, XML_OPTION_SKIP_WHITE and XML_OPTION_TARGET_ENCODING.
$value: (Required) It is used to specify options new value.
XML Parser Options
The below options are available:
XML_OPTION_CASE_FOLDING (Data type: bool): Controls if the parser converts tag names to uppercase. Enabled by default.
XML_OPTION_PARSE_HUGE (Data type: bool): Allows the parser to handle XML documents larger than 10 MB. Use carefully, as it can cause security issues if the file size is not controlled. This option requires libxml2.
XML_OPTION_SKIP_TAGSTART (Data type: integer): Specifies how many characters to skip at the start of a tag name.
XML_OPTION_SKIP_WHITE (Data type: bool): Decides whether to ignore values that only have spaces or other whitespace characters.
XML_OPTION_TARGET_ENCODING (Data type: string): Sets the character encoding for the parsed XML data. Default is the same as the source encoding. Supported options: ISO-8859-1, US-ASCII, and UTF-8.
Return Value
The xml_parser_set_option() function returns TRUE on success. And FALSE on failure.
PHP Version
First introduced in core PHP 4, the xml_parser_set_option() 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_parser_set_option() function. The below program shows how to enable or disable case folding in an XML parser. When case folding is enabled, all tags are automatically converted to uppercase. To change this, use XML_OPTION_CASE_FOLDING.
<?php // Create an XML parser $parser = xml_parser_create(); // Disable case folding xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false); // Free the parser echo xml_parser_free($parser); ?>
Output
Here is the outcome of the following code −
1
Example 2
Below is the complete code demonstrating the usage of xml_parser_set_option() and XML_OPTION_SKIP_TAGSTART with a working example. In this we are skipping the first few characters of tag names using XML_OPTION_SKIP_TAGSTART and it modifies how the parser reads tags for custom formatting.
<?php // Create an XML parser $parser = xml_parser_create(); // Skip the first 3 characters of tag names xml_parser_set_option($parser, XML_OPTION_SKIP_TAGSTART, 3); // Define a handler for start tags function startElement($parser, $name, $attrs) { echo "Start Tag: $name\n"; } // Define a handler for end tags function endElement($parser, $name) { echo "End Tag: $name\n"; } // Define a handler for character data function characterData($parser, $data) { echo "Character Data: " . trim($data) . "\n"; } // Set the handlers for the parser xml_set_element_handler($parser, "startElement", "endElement"); xml_set_character_data_handler($parser, "characterData"); // XML string to parse $xmlData = <<<XML <abcRoot> <abcChild>Value</abcChild> </abcRoot> XML; // Parse the XML string if (!xml_parse($parser, $xmlData, true)) { die(sprintf( "XML error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser) )); } // Free the parser xml_parser_free($parser); ?>
Output
This will generate the below output −
Start Tag: ROOT Character Data: Start Tag: CHILD Character Data: Value End Tag: CHILD Character Data: End Tag: ROOT