Is there any function that makes string from PHP SimpleXMLElement
?

- 5,077
- 3
- 27
- 34

- 4,413
- 13
- 35
- 38
-
4What kind of string do you want? – Shamim Hafiz - MSFT Sep 11 '10 at 12:18
8 Answers
You can use the SimpleXMLElement::asXML()
method to accomplish this:
$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);
// The entire XML tree as a string:
// "<element><child>Hello World</child></element>"
$xml->asXML();
// Just the child node as a string:
// "<child>Hello World</child>"
$xml->child->asXML();
-
22
-
-
2but yo are left with the tags, what if you don't want the tags? other answers solve this problem – Andrew Jan 26 '16 at 20:46
-
Don't use @LukeSnowden's method as this isn't supposed to be called directly. The actual correct answer is Nicolay77's answer which uses casting. – user2924019 Feb 15 '22 at 16:45
You can use casting:
<?php
$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);
$text = (string)$xml->child;
$text will be 'Hello World'

- 2,085
- 25
- 20
Actually asXML() converts the string into xml as it name says:
<id>5</id>
This will display normally on a web page but it will cause problems when you matching values with something else.
You may use strip_tags function to get real value of the field like:
$newString = strip_tags($xml->asXML());
PS: if you are working with integers or floating numbers, you need to convert it into integer with intval() or floatval().
$newNumber = intval(strip_tags($xml->asXML()));

- 583
- 9
- 12
You can use ->child
to get a child element named child.
This element will contain the text of the child element.
But if you try var_dump()
on that variable, you will see it is not actually a PHP string.
The easiest way around this is to perform a strval(xml->child);
That will convert it to an actual PHP string.
This is useful when debugging when looping your XML and using var_dump()
to check the result.
So $s = strval($xml->child);
.
Here is a function I wrote to solve this issue (assuming tag has no attributes). This function will keep HTML formatting in the node:
function getAsXMLContent($xmlElement)
{
$content=$xmlElement->asXML();
$end=strpos($content,'>');
if ($end!==false)
{
$tag=substr($content, 1, $end-1);
return str_replace(array('<'.$tag.'>', '</'.$tag.'>'), '', $content);
}
else
return '';
}
$string = "<element><child>Hello World</child></element>";
$xml = new SimpleXMLElement($string);
echo getAsXMLContent($xml->child); // prints Hello World

- 183
- 2
- 7
Sometimes you can simply typecast:
// this is the value of my $xml
object(SimpleXMLElement)#10227 (1) {
[0]=>
string(2) "en"
}
$s = (string) $xml; // returns "en";

- 7,426
- 12
- 73
- 118
-
2This is the kind of object you get for the description field of an RSS feed. It's counter-intuitive that it works, but it was the only solution here that gave me the string inside `$xmlitem->description`. Thanks. – Henrik Erlandsson Aug 08 '19 at 09:12
Probably depending on the XML feed you may/may not need to use __toString()
; I had to use the __toString()
otherwise it is returning the string inside an SimpleXMLElement. Maybe I need to drill down the object further ...

- 19,824
- 17
- 99
- 186

- 594
- 6
- 9