PHP xml_get_current_byte_index() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
XML Parser

Definition

In PHP, xml_get_current_byte_index() tells you the byte position the XML parser has reached in the data you gave to xml_parse().

Call it inside a handler to find where the current tag or text begins. The count starts at 0 and grows across all chunks, so it works even when you feed the XML in pieces.

Syntax

Call xml_get_current_byte_index() with the values you need. Start with this line.

PHP
$byte = xml_get_current_byte_index($parser);
CallWhat it does
xml_get_current_byte_index($parser)Returns the byte offset of the current parser event.
substr($xml, xml_get_current_byte_index($parser), 6)Cuts the original string at the position where the event started.
xml_get_current_byte_index($parser) + 1Shows a 1-based position if you want to display it to a person.

The position counts bytes, not characters. A letter such as é uses 2 bytes in UTF-8, so the number can be larger than the character count.

Library declarations
PHP
xml_get_current_byte_index(XMLParser $parser): int

Parameters

ParameterDescription
parserRequired. The XMLParser object created by xml_parser_create() or xml_parser_create_ns().

Return Value

Return ValueDescription
intThe zero-based byte index of the current parser position.

PHP Examples

Example 1 — Find Where Each Tag Starts

Set an element handler and print the byte index each time a start tag is found.

PHP
<?php
$xml = '<root><item id="1">Tea</item></root>';

$parser = xml_parser_create();
xml_set_element_handler(
    $parser,
    function ($parser, $name) {
        echo $name, " starts at byte ", xml_get_current_byte_index($parser), PHP_EOL;
    },
    function ($parser, $name) {
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. xml_parser_create() makes the parser, and xml_set_element_handler() registers the start and end handlers.
  2. xml_parse() reads the XML and calls the start handler for ROOT and ITEM.
  3. Inside the handler, xml_get_current_byte_index() returns where that tag begins.
  4. Tag names are upper case because the parser folds case by default.

Example 2 — Bytes Are Not Characters

The letter é takes two bytes in UTF-8. Use the byte index with substr() to see that it points to the exact start of the second tag.

PHP
<?php
$xml = '<root><item>é</item><item>x</item></root>';
$count = 0;

$parser = xml_parser_create();
xml_set_element_handler(
    $parser,
    function ($parser, $name) use ($xml, &$count) {
        if ($name !== 'ITEM') {
            return;
        }

        $count++;
        if ($count === 2) {
            $index = xml_get_current_byte_index($parser);
            echo "Second ITEM at byte ", $index, PHP_EOL;
            echo "Slice: ", substr($xml, $index, 6), PHP_EOL;
        }
    },
    function ($parser, $name) {
    }
);

xml_parse($parser, $xml, true);

How It Works

  1. The handler ignores ROOT and counts only ITEM start tags.
  2. On the second ITEM it reads the byte index of that tag.
  3. The é uses 2 bytes, so the index is 21 even though only 20 characters come before the tag.
  4. substr() uses the same byte positions, so it returns exactly the <item> tag.

Example 3 — Byte Index Across Chunks

When you send XML in two chunks, the byte index keeps counting from the start of the whole document.

PHP
<?php
$chunks = [
    '<root><item>Tea</item>',
    '<item>Milk</item></root>',
];

$parser = xml_parser_create();
xml_set_element_handler(
    $parser,
    function ($parser, $name) {
        if ($name === 'ITEM') {
            echo "ITEM at byte ", xml_get_current_byte_index($parser), PHP_EOL;
        }
    },
    function ($parser, $name) {
    }
);

foreach ($chunks as $i => $chunk) {
    xml_parse($parser, $chunk, $i === array_key_last($chunks));
}

How It Works

  1. The XML is split into two strings, and both go to the same parser.
  2. The first chunk is 22 bytes long, so the second chunk starts at byte 22 of the document.
  3. xml_get_current_byte_index() adds the earlier chunks to its count.
  4. The last chunk is sent with is_final set to true to finish the document.

Frequently Asked Questions

It returns the zero-based byte position of the current parser event inside the XML data. The first byte of the document is 0.
Bytes. Characters such as é or emoji use more than one byte in UTF-8, so the byte index can be larger than the character position.
Call it inside an element or character data handler, or right after xml_parse() returns 0 to find where an error happened.
No. The count keeps growing across chunks, so the number always refers to a position in the full document.
The byte index is a single number from the start of the document. xml_get_current_line_number() and xml_get_current_column_number() give a human-friendly position made of a line and a column.

Next: xml_get_current_column_number()

xml_get_current_column_number() returns the column where the XML parser currently is on th.

xml_get_current_column_number() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful