PHP xml_get_current_column_number() Function



The PHP XML Parser xml_get_current_column_number() function is a built-in PHP function that returns the current column number of a specified parser. This method is available for PHP 4.0.0 and later versions. These examples may not work with online IDEs. So, try running it on a local server or a PHP-hosted server.

Syntax

Below is the syntax of the PHP XML Parser xml_get_current_column_number() function −

int xml_get_current_column_number ( XMLParser $xml_parser )

Parameters

This function accepts $xml_parser parameter which is a reference to the XML parser used to obtain column numbers.

Return Value

The xml_get_current_column_number() function returns the column on the current line where the parser is now.

PHP Version

First introduced in core PHP 4, the xml_get_current_column_number() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

This program shows how to use the PHP XML Parser xml_get_current_column_number() function to get the current column of the XML parser at the time of processing a simple XML. As it is showing the column number for each start element in the XML.

<?php
   // Define XML content
   $xmlData = "<root><child>Content</child></root>";

   // Create an XML parser
   $p = xml_parser_create();

   // Function to handle XML data
   function startElement($p, $name, $attrs) {
      echo "Start element: $name at column " . xml_get_current_column_number($p) . "\n";
   }

   // Set element handler
   xml_set_element_handler($p, "startElement", null);

   // Parse the XML
   if (!xml_parse($p, $xmlData, true)) {
      echo "Error in XML parsing";
   }

   // Free the parser
   xml_parser_free($p);
?>

Output

Here is the outcome of the following code −

Start element: ROOT at column 6
Start element: CHILD at column 13

Example 2

This example parses a more complex XML structure using the xml_get_current_column_number() function and returns the column numbers for each tag. It parses larger XML files and displays the column number for each element tag it encounters.

<?php
   // Define XML content
   $xmlData = <<<XML
   <books>
      <book>
         <title>PHP Programming</title>
      </book>
      <book>
         <title>Advanced XML</title>
      </book>
   </books>
   XML;

   // Create parser
   $p = xml_parser_create();

   // Function to handle XML start elements
   function startElement($p, $name, $attrs) {
      echo "Processing <$name> at column " . xml_get_current_column_number($p) . "\n";
   }

   // Set handler for start elements
   xml_set_element_handler($p, "startElement", null);

   // Parse the XML data
   if (!xml_parse($p, $xmlData, true)) {
      echo "Error: " . xml_error_string(xml_get_error_code($p));
   }

   // Free parser
   xml_parser_free($p);
?> 

Output

This will generate the below output −

Processing <BOOKS> at column 7
Processing <BOOK> at column 10
Processing <TITLE> at column 15
Processing <BOOK> at column 10
Processing <TITLE> at column 15

Example 3

This example shows how to handle XML errors by identifying their location in the document while using the xml_get_current_column_number() function. This program parses an invalid XML file and returns the column and line number where the error occurred.

<?php
   // Define XML content
   $xmlData = "<root><item>Test</item><error></root>"; // Invalid XML

   // Create parser
   $p = xml_parser_create();

   // Parse XML and handle errors
   if (!xml_parse($p, $xmlData, true)) {
      echo "Error at line " . xml_get_current_line_number($p) .
         ", column " . xml_get_current_column_number($p) . ": " .
         xml_error_string(xml_get_error_code($p)) . "\n";
   }

   // Free parser
   xml_parser_free($p);
?> 

Output

This will create the below output −

Error at line 1, column 38: Mismatched tag
php_function_reference.htm
Advertisements