PHP | xml_parser_set_option() Function

Last Updated : 31 Oct, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
Pre-requisite: XML Basics The xml_parser_set_option() function is an inbuilt function in PHP which is used to set the options in an XML parser. Syntax:
bool xml_parser_set_option( resource $parser,
           int $specified_option, mixed $option_value)
Parameters: This function accepts three parameters as mentioned above and described below:
  • $parser: It is required parameter which specifies that the XML parser whose options to be set.
  • $specified_option: It is required parameter which specifies the options to be set for specified parser. Possible values of this parameter are:
    • XML_OPTION_CASE_FOLDING: It is used to check whether case-folding is enabled or not. The value 1 represents enable and 0 represents disable value.
    • XML_OPTION_TARGET_ENCODING: It specifies the target encoding in the specified XML parser. Set the name of the encoding (US-ASCII, UTF-8 or ISO-8859-1 etc).
    • XML_OPTION_SKIP_TAGSTART: It specifies the number of characters is skipped in the beginning of a tag name.
    • XML_OPTION_SKIP_WHITE: It is used to check whether the whitespace characters are skipped or not. The value 1 is used to skip and 0 otherwise.
  • $option_value: It is required parameter which specifies that a new value for the specified option to be set.
Return Value: It returns True on success or False on failure. Note: This function is available for PHP 4.0.0 and newer version. Program 1: php
<?php

// Creating XML parser
$parser = xml_parser_create();

// Set the option XML_OPTION_CASE_FOLDING
$res = xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

if( $res ){
    
    // On success
    echo "option XML_OPTION_CASE_FOLDING has successfully been set!<br>";
}
else {
    
    // On failure
    echo "error while setting option XML_OPTION_CASE_FOLDING!<br>";
}

// Setting the option XML_OPTION_TARGET_ENCODING
$res = xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');

if($res) {

    // On success
    echo "option XML_OPTION_TARGET_ENCODING has successfully been set!";
}
else {
    
    // On failure
    echo "error while setting option XML_OPTION_TARGET_ENCODING!";
}

// Free to XML parser
xml_parser_free($parser);

?>
Output:
option XML_OPTION_CASE_FOLDING has successfully been set!
option XML_OPTION_TARGET_ENCODING has successfully been set!
Program 2: This program display the result on wrong value. php
<?php

// Creating an XML parser
$parser = xml_parser_create();

// Setting the option XML_OPTION_TARGET_ENCODING
$res = xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, '0');

if($res) {
    
    // On success
    echo "option XML_OPTION_TARGET_ENCODING has successfully been set!";
}
else {
    
    // On failure
    echo "error while setting option XML_OPTION_TARGET_ENCODING!";
}

// Free to XML parser
xml_parser_free($parser);

?>
Note: A runtime error will occur for this example as the value is invalid for the option. Output:
error while setting option XML_OPTION_TARGET_ENCODING!
Reference: https://www.php.net/manual/en/function.xml-parser-set-option.php

Next Article

Similar Reads