PHP | xml_get_current_byte_index() Function

Pre-requisite: XML Basics

The xml_get_current_byte_index() function is an inbuilt function in PHP which is used to return the byte index of an XML parser.

Syntax:



int xml_get_current_byte_index( resource $parser)

Parameters: This function accepts single parameter $xml_parser which is required. This parameter specifies the XML parser to use.

Return Values: This function returns the current byte index of specified parser on success or False on failure.

Note: This function is available for PHP 4.0.0 and newer version.

Below programs illustrate the xml_get_current_byte_index() function in PHP:

gfg.xml file (Mismatch tags):

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0" encoding="utf-8"?>
<user>
    <username> user123 </username>
    <name> firstname lastname </name>
    <phone> +91-9876543210 </phone>
    <detail> I am John Doe. Live in Kolkata, India. </detail>
</users>

chevron_right


Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Invalid XML file
$xml_file= 'gfg.xml';
   
// XML parser initialization
$xml_parser = xml_parser_create();
    
// Opening the file in read mode
$file_pointer = fopen($xml_file, 'r');
    
// Reading data from the file stream
while ($xml_data = fread($file_pointer, 4096)) {
     
    // Parsing the data chunk
    if (!xml_parse($xml_parser, $xml_data, feof($file_pointer))) {
         
        // Display error 
        die( print "ERROR: "
           
            // Error string
            xml_error_string(xml_get_error_code($xml_parser)) . 
               
            "<br>Error Code: "
               
            // Error code
            xml_get_error_code($xml_parser) . 
               
            "<br>Line: "
               
            // Line number where the error occurred    
            xml_get_current_line_number($xml_parser) . 
               
            "<br>Column: "
               
            // Column number where the error occurred    
            xml_get_current_column_number($xml_parser) . 
               
            "<br>Byte Index: "
               
            // Byte index where the current byte occurred
            xml_get_current_byte_index($xml_parser) . "<br>"
        );
    }
}
   
// Free to xml parser
xml_parser_free($xml_parser);
   
?>

chevron_right


Output:

ERROR: Mismatched tag
Error Code: 76
Line: 7
Column: 13
Byte Index: 208

geeks.xml file:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?xml version="1.0 encoding="utf-8"?>
<user>
    <username> user123 </username>
    <name> firstname lastname </name>
    <phone> +91-9876543210 </phone>
    <detail> I am John Doe. Live in Kolkata, India. </detail>
</user>

chevron_right


Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Invalid XML file
$xml_file= 'gfg.xml';
   
// XML parser initialization
$xml_parser = xml_parser_create();
    
// Opening the file in read mode
$file_pointer = fopen($xml_file, 'r');
    
// Reading data from the file stream
while ($xml_data = fread($file_pointer, 4096)) {
     
    // Parsing the data chunk
    if (!xml_parse($xml_parser, $xml_data, feof($file_pointer))) {
         
        // Display error 
        die( print "ERROR: "
           
            // Error string
            xml_error_string(xml_get_error_code($xml_parser)) . 
               
            "<br>Error Code: "
               
            // Error code
            xml_get_error_code($xml_parser) . 
               
            "<br>Line: "
               
            // Line number where the error occurred    
            xml_get_current_line_number($xml_parser) . 
               
            "<br>Column: "
               
            // Column number where the error occurred    
            xml_get_current_column_number($xml_parser) . 
               
            "<br>Byte Index: "
               
            // Byte index where the current byte occurred
            xml_get_current_byte_index($xml_parser) . "<br>"
        );
    }
}
   
// Free to xml parser
xml_parser_free($xml_parser);
   
?>

chevron_right


Output:

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

Reference: https://www.php.net/manual/en/function.xml-get-current-byte-index.php



My Personal Notes arrow_drop_up

I am a programmer by hobby

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Akanksha_Rai



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.