Skip to content
Related Articles

Related Articles

PHP | SimpleXMLElement __toString() Function
  • Last Updated : 21 Mar, 2020
GeeksforGeeks - Summer Carnival Banner

The XMLReader::__toString() function is an inbuilt function in PHP which is used to get the text content that is directly in current element. This function does not return text content that is inside this element’s children.

Syntax:

void XMLReader::__toString( void )

Parameters: This function doesn’t accept any parameters.

Return Value: This function returns a string content on success or an empty string on failure.

Below examples illustrate the XMLReader::__toString() function in PHP:



Example 1:




<?php
  
// Create the XML
$xmlstr = <<<XML
<?xml version='1.0'?>
<library>
  GeeksforGeeks
</library>
XML;
  
// Create a new SimpleXMLElement
$SXE = new SimpleXMLElement($xmlstr);
  
// Get the content
$string = $SXE->__toString();
echo $string;
?>

Output:

GeeksforGeeks

Example 2:




<?php
  
// Create the XML
$xmlstr = <<<XML
<?xml version='1.0'?>
<div>
  <sub>
  This text should not be visible
  </sub>
</div>
XML;
  
// Create a new SimpleXMLElement
$SXE = new SimpleXMLElement($xmlstr);
  
// Add the attribute
$string = $SXE->__toString();
echo $string;
?>

Output:

// Empty string because text content is in a childern node not current node.

Reference: https://www.php.net/manual/en/simplexmlelement.tostring.php

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :