97

Is there any function that makes string from PHP SimpleXMLElement?

1
  • 4
    What kind of string do you want? Commented Sep 11, 2010 at 12:18

8 Answers 8

143

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(); 
Sign up to request clarification or add additional context in comments.

4 Comments

$string = $xml->child->__toString();
htmlspecialchars( $xml->asXML() ) ?
but yo are left with the tags, what if you don't want the tags? other answers solve this problem
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.
75

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'

1 Comment

Works for me because it doesn't put the tags in, thanks
18

You can use the asXML method as:

<?php // string to SimpleXMLElement $xml = new SimpleXMLElement($string); // make any changes. .... // convert the SimpleXMLElement back to string. $newString = $xml->asXML(); ?> 

Comments

17

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())); 

Comments

9

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);.

Comments

6

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 

Comments

2

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"; 

1 Comment

This 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.
1

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 ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.