PHP Function Reference

PHP xml_get_current_byte_index() Function



The PHP xml_get_current_byte_index() function returns the current byte index of the given XML parser. The function returns false if parser does not refer to a valid parser, or else it returns the current byte index of the parser in its data buffer (starting at 0).

Note: This function returns byte index according to UTF-8 encoded text disregarding if input is in another encoding.

Syntax

xml_get_current_byte_index(parser)

Parameters

parser Required. Specify a reference to the XML parser to get byte index from.

Return Value

Returns false if parser does not refer to a valid parser, or else it returns the current byte index of the parser in its data buffer (starting at 0).

Example: Error (String do not enclosed in double quotes)

Lets assume that we have a file called demo.xml. This file contains following content. The content contains error (string which enclosed in double quotes).

<?xml version="1.0 encoding="UTF-8"?>
<userlist>
  <user>
    <username>John123</username>
    <name>John Smith</name>
    <phone>+1-8054098000</phone>
    <address>Brooklyn, New York, USA</address>
  </user>
</userlist>  

In the example below, the XML file is parsed. It displays the error while parsing the document.

<?php
//create an XML parser
$parser=xml_parser_create();

//opening xml file
$fp = fopen("demo.xml", "r");

while($data = fread($fp,4096)) {
  //parsing XML data
  if(!xml_parse($parser, $data, feof($fp))) {
    //displaying errors
    die(print "Error: " .
      //error string
      xml_error_string(xml_get_error_code($parser)) .
       
      "<br/>Error Code: " . 
      //error code
      xml_get_error_code($parser) .
       
      "<br/>Line: " .
      //line number where the error occurred
      xml_get_current_line_number($parser) .
       
      "<br/>Column: " .
      //column number where the error occurred
      xml_get_current_column_number($parser) .
       
      "<br/>Byte Index: " .
      //byte index where the error occurred
      xml_get_current_byte_index($parser) . "<br/>"
    );
  }
}

//free XML parser
xml_get_error_coder_free($parser);

fclose($fp);
?>

The output of the above code will be:

Error: String not closed expecting " or '
Error Code: 34
Line: 1
Column: 38
Byte Index: 37

Example: Error (Mismatched tag)

Lets assume that we have a file called example.xml. This file contains following content. The content contains error (mismatched tag).

<?xml version="1.0" encoding="UTF-8"?>
<userlist>
  <user>
    <username>John123</username>
    <name>John Smith</name>
    <phone>+1-8054098000</phone>
    <address>Brooklyn, New York, USA<address>
  </user>
</userlist>  

In the example below, the XML file is parsed. It displays the error while parsing the document.

<?php
//create an XML parser
$parser=xml_parser_create();

//opening xml file
$fp = fopen("example.xml", "r");

while($data = fread($fp,4096)) {
  //parsing XML data
  if(!xml_parse($parser, $data, feof($fp))) {
    //displaying errors
    die(print "Error: " .
      //error string
      xml_error_string(xml_get_error_code($parser)) .
       
      "<br/>Error Code: " . 
      //error code
      xml_get_error_code($parser) .
       
      "<br/>Line: " .
      //line number where the error occurred
      xml_get_current_line_number($parser) .
       
      "<br/>Column: " .
      //column number where the error occurred
      xml_get_current_column_number($parser) .
       
      "<br/>Byte Index: " .
      //byte index where the error occurred
      xml_get_current_byte_index($parser) . "<br/>"
    );
  }
}

//free XML parser
xml_get_error_coder_free($parser);

fclose($fp);
?>

The output of the above code will be:

Error: Mismatched tag
Error Code: 76
Line: 8
Column: 10
Byte Index: 220

❮ PHP XML Parser Reference