xml_get_current_column_number()
The xml_get_current_column_number() function is a PHP built-in function that retrieves the current column number of an XML parser. When parsing an XML file
The xml_get_current_column_number() function is a PHP built-in function that retrieves the current column number of an XML parser — the horizontal character offset (1-based) within the current line that the parser has reached. It belongs to the legacy XML Parser (Expat) extension, not to SimpleXML or DOM, and is only meaningful while a parse is in progress.
This page covers what the function returns, its syntax, and runnable examples that show the column number reported from inside SAX event handlers. It is most useful for pinpointing the location of an element or error in the input — for example, to write a precise log message or a helpful "syntax error near column X" diagnostic.
Because the value reflects the parser's live position, you must call it from inside a handler (or right after xml_parse() fails). It pairs naturally with xml_get_current_line_number() and xml_get_current_byte_index() to fully describe a position.
Syntax
xml_get_current_column_number(XMLParser $parser): int|false$parser is the XML parser created by xml_parser_create() (in PHP 8.0+ this is an XMLParser object; in earlier versions it was a resource). The function returns the column number as an integer, or false if $parser is not a valid parser.
Usage Examples
Let's take a look at some practical examples of using xml_get_current_column_number() in PHP.
Example 1: Retrieving the Current Column Number of an XML Parser
Suppose you have an XML string that you want to parse using the legacy XML Parser extension in PHP. You can call xml_get_current_column_number() inside a SAX event handler — registered with xml_set_element_handler() — to retrieve the column where each tag begins:
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, function($parser, $name) {
echo "Start tag: $name at column " . xml_get_current_column_number($parser) . "\n";
}, function($parser, $name) {
echo "End tag: $name\n";
});
$xml_data = "<root>\n <item>Test</item>\n</root>";
xml_parse($xml_parser, $xml_data);
xml_parser_free($xml_parser);This code creates a parser, registers start/end element handlers, and runs xml_parse() over the data. Inside the start-tag handler it reads the live column number and prints it, then frees the parser with xml_parser_free(). Note that the column points to the character after the tag opener, so it advances as the parser consumes each tag. The output is:
Start tag: ROOT at column 6
Start tag: ITEM at column 8
End tag: ITEM
End tag: ROOTExample 2: Tracking Column Numbers During Parsing
Suppose you need to track the column position of text content while parsing an XML file. Because xml_parse_into_struct() does not populate column numbers in its output array, real-time tracking requires SAX-style event handlers rather than post-parse iteration. You can register a character-data handler with xml_set_character_data_handler() to capture column positions as the parser encounters text:
$xml_parser = xml_parser_create();
xml_set_character_data_handler($xml_parser, function($parser, $data) {
$col = xml_get_current_column_number($parser);
echo "Text content found at column: $col\n";
});
$xml_data = "<root>\n <item>Sample Data</item>\n</root>";
xml_parse($xml_parser, $xml_data);
xml_parser_free($xml_parser);The character-data handler may fire more than once per text node (for example, around whitespace and line breaks), so you typically see several reported columns. This approach tracks positions in real time, unlike post-parse array iteration, which cannot access dynamic parser state. The output for this input is:
Text content found at column: 3
Text content found at column: 20
Text content found at column: 1Notes and gotchas
- Call it during parsing. The value only makes sense while
xml_parse()is running (inside a handler) or immediately after a parse error. Outside that window it has no useful meaning. - Columns are 1-based and per line. The count resets at each newline. Combine it with
xml_get_current_line_number()to get a completeline:columnlocation. - Byte index vs. column. If you need the absolute offset from the start of the document instead of the per-line position, use
xml_get_current_byte_index(). - Legacy extension. The Expat-based XML Parser is procedural and event-driven. For most modern document handling, prefer SimpleXML or DOM — but for streaming large files or precise error locations, this extension is still the right tool.
Conclusion
In this article, we've discussed PHP's xml_get_current_column_number() function and how it can be used to retrieve the current column number of an XML parser in PHP. We've explained what the function does, its syntax, and provided examples of how it can be used in practical scenarios. By following these examples, you can easily track the current position of an XML parser during parsing and use it in your PHP applications for logging, debugging, or progress tracking.