Monday, December 22, 2025
HomeLanguagesPHP | DOMNode C14N() Function

PHP | DOMNode C14N() Function

The DOMNode::C14N() function is an inbuilt function in PHP which is used to convert a node into string.

Syntax:

string DOMNode::C14N( bool $exclusive, 
bool $with_comments, array $xpath, array $ns_prefixes )

Parameters: This function accepts four parameters as mentioned above and described below:

  • $exclusive (Optional): It specifies whether to enable exclusive parsing of only the nodes matched by the provided xpath or namespace prefixes.
  • $with_comments (Optional): It specifies whether to retain comments in output.
  • $xpath (Optional): It specifies an array of xpaths to filter the nodes by.
  • $ns_prefixes (Optional): It specifies an array of namespace prefixes to filter the nodes by.

Return Value: This function returns canonicalized nodes as a string or FALSE on failure.

Below examples illustrate the DOMNode::C14N() function in PHP:

Example 1:




<?php
  
// Create a DOMDocument
$doc = new DOMDocument();
  
// Load XML
$doc->loadXML('<html></html>');
  
// Create an heading element on 
// DOMDocument object
$h1 = $doc->createElement('h1');
  
// Append the child
$doc->documentElement->appendChild($h1);
  
// Get the data without comments
$stringdata = $doc->C14N();
echo $stringdata;
?>


Output:

<html><h1></h1></html>

Example 2:




<?php
// Create a DOMDocument
$doc = new DOMDocument();
  
// Load XML
$doc->loadXML('<html><!-- This is a comment --></html>');
  
// Create an heading element on DOMDocument object
$h1 = $doc->createElement('h1');
  
// Append the child
$doc->documentElement->appendChild($h1);
  
// Get the data with comments
$stringdata = $doc->C14N(false, true);
echo 'With Comments: <br>';
echo htmlentities($stringdata);
  
// Get the data without comments
$stringdata = $doc->C14N(false, false);
echo '<br>Without Comments: <br>';
echo htmlentities($stringdata);
?>


Output:

With Comments: 
<html><!-- This is a comment --><h1></h1></html>
Without Comments:
<html><h1></h1></html>

Reference: https://www.php.net/manual/en/domnode.c14n.php

RELATED ARTICLES

Most Popular

Dominic
32456 POSTS0 COMMENTS
Milvus
111 POSTS0 COMMENTS
Nango Kala
6823 POSTS0 COMMENTS
Nicole Veronica
11958 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12038 POSTS0 COMMENTS
Shaida Kate Naidoo
6958 POSTS0 COMMENTS
Ted Musemwa
7203 POSTS0 COMMENTS
Thapelo Manthata
6911 POSTS0 COMMENTS
Umr Jansen
6890 POSTS0 COMMENTS