Forcing a SimpleXML Object to a string, regardless of context

Solution 1:

Typecast the SimpleXMLObject to a string:

$foo = array( (string) $xml->channel->item->title );

The above code internally calls __toString() on the SimpleXMLObject. This method is not publicly available, as it interferes with the mapping scheme of the SimpleXMLObject, but it can still be invoked in the above manner.

Solution 2:

You can use the PHP function

strval();

This function returns the string values of the parameter passed to it.

Solution 3:

There is native SimpleXML method SimpleXMLElement::asXML Depending on parameter it writes SimpleXMLElement to xml 1.0 file or just to a string:

$xml = new SimpleXMLElement($string);
$validfilename = '/temp/mylist.xml';
$xml->asXML($validfilename);    // to a file
echo $xml->asXML();             // to a string

Solution 4:

Another ugly way to do it:

$foo = array( $xml->channel->item->title."" );

It works, but it's not pretty.