Open In App

PHP | DOMDocument createEntityReference() Function

Last Updated : 27 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

The DOMDocument::createEntityReference() function is an inbuilt function in PHP which is used to create a new instance of class DOMEntityReference.

Syntax:

DOMEntityReference DOMDocument::createEntityReference( string $name )

Parameters: This function accepts single parameter $name which holds the content of the entity reference. The entity reference does not contains leading & and the trailing ; characters.

Returns Value: This function returns the new DOMEntityReference object on success or FALSE on failure.

Below programs illustrate the DOMDocument::createEntityReference() function in PHP:

Program 1:




<?php
  
// Create a new DOMDocument object
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createEntityReference() function to create
// new entity reference node
$domER = $domDocument->createEntityReference('nbsp');
  
// Append element to the document
$domDocument->appendChild($domER);
  
// Save the XML document and display it
echo $domDocument->saveXML();
  
?>


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
&nbsp;

Program 2:




<?php
  
// Create a new DOMDocument object
$domDocument = new DOMDocument('1.0', 'iso-8859-1');
  
// Use createEntityReference() function to create
// new entity reference node
$domER1 = $domDocument->createEntityReference('amp');
$domER2 = $domDocument->createEntityReference('lt');
$domER3 = $domDocument->createEntityReference('gt');
$domER4 = $domDocument->createEntityReference('reg');
  
// Append element to the document
$domDocument->appendChild($domER1);
$domDocument->appendChild($domER2);
$domDocument->appendChild($domER3);
$domDocument->appendChild($domER4);
  
// Save the XML document and display it
echo $domDocument->saveXML();
  
?>


Output:

<?xml version="1.0" encoding="iso-8859-1"?>
&amp;
&lt;
&gt;
&reg;

Reference: https://www.php.net/manual/en/domdocument.createentityreference.php



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads