In PHP, xml_parser_set_option() changes a setting on an XML parser, such as case folding, tag-start skipping, whitespace skipping, or the target encoding.
Call it after xml_parser_create() and before xml_parse(). By default PHP turns every tag name into UPPERCASE for your handlers. With this function you can switch that off, trim a fixed prefix from tag names, or choose the encoding used for text passed to your handlers.
Syntax
Call xml_parser_set_option() with the values you need. Start with this line.
Chooses the encoding used for strings handed to your handlers.
Four options exist: XML_OPTION_CASE_FOLDING (on by default), XML_OPTION_SKIP_TAGSTART (default 0), XML_OPTION_SKIP_WHITE (default 0), and XML_OPTION_TARGET_ENCODING (UTF-8 unless you chose another one in xml_parser_create()). Use xml_parser_get_option() to read a value back.
Library declarations
PHP
xml_parser_set_option(XMLParser $parser, int $option, string|int|bool $value): bool
Parameters
Parameter
Description
parser
Required. The XMLParser object returned by xml_parser_create() or xml_parser_create_ns().
option
Required. One of XML_OPTION_CASE_FOLDING, XML_OPTION_SKIP_TAGSTART, XML_OPTION_SKIP_WHITE, or XML_OPTION_TARGET_ENCODING.
value
Required. The new value. Use a bool or int for the first three options and an encoding name such as UTF-8, ISO-8859-1, or US-ASCII for the target encoding.
Return Value
Return Value
Description
bool
true when the option was changed. In PHP 8 an unknown option or an unsupported encoding name throws a ValueError instead of returning false.
PHP Examples
Example 1 — Turn Case Folding On and Off
Collect the start-tag names from one tiny XML string, once with the default folding and once with folding switched off.
Encoding: ISO-8859-1
Skip white: 1
Case folding: 1
Bad encoding: ValueError
How It Works
The target encoding is set to ISO-8859-1 and whitespace skipping is switched on.
xml_parser_get_option() reads the values back, and the untouched case folding option still shows its default of 1.
Passing NOT-AN-ENCODING as the target encoding is not allowed in PHP 8.
PHP throws a ValueError, which the catch block reports by printing the class name.
Frequently Asked Questions
It changes one setting of an XMLParser object. You can control case folding, how many characters to skip at the start of tag names, whether whitespace-only values are skipped, and which encoding is used for text passed to your handlers.
XML_OPTION_CASE_FOLDING is on by default, so PHP converts tag and attribute names to uppercase before calling your handlers. Call xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, false) to keep the original case.
Call it after creating the parser and before the first xml_parse() call. Changing an option in the middle of a parse is not a supported way to work.
PHP supports UTF-8, ISO-8859-1, and US-ASCII as target encodings. Any other name throws a ValueError in PHP 8.
Use xml_parser_get_option() with the same XML_OPTION_* constant. It returns the current integer or string value of that option.