Here\'s my XML data:
$data = \'Report of the Fifth International Foo and Bar Confe
the docs for __toString
on SimpleXMLElement says: "Returns text content that is directly in this element. Does not return text content that is inside this element's children."
The asXML
method seems to be better for what you want: http://php.net/manual/en/simplexmlelement.asxml.php
it will return a string:
"<?xml version="1.0"?>
<title>Report of the <org reg="International Foo and Bar Conference, 5th">Fifth International Foo and Bar Conference</org>, <org>Foobar Hall</org>, London, July 14 to 16, 1908.</title>
"
https://eval.in/410230
You'd have to strip out the opening XML tag though, but it's much better.
This can be easily done in DOM. DOM element nodes have a property $textContent, that will return its text content including all descendant text nodes.
$document = new DOMDocument();
$document->loadXml($data);
var_dump($document->documentElement->textContent);
Output:
string(99) "Report of the Fifth International Foo and Bar Conference, Foobar Hall, London, July 14 to 16, 1908."
If you do not have the element node already in a variable, it will be more convenient to use XPath.
$document = new DOMDocument();
$document->loadXml($data);
$xpath = new DOMXpath($document);
var_dump($xpath->evaluate('string(/title)'));
It is even possible to convert a SimpleXMLElement
into a DOM element node.
$element = new SimpleXMLElement($data);
$node = dom_import_simplexml($element);
var_dump($node->textContent);