note 96618 deleted from simplexmlelement.asXML by danbrown
| From: | danbrown@php.net | Date: | Mon, 08 Mar 2010 19:01:00 +0000 |
| Subject: | note 96618 deleted from simplexmlelement.asXML by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166796@lists.php.net to get a copy of this message | ||
Note Submitter: qosys dot ney at gmail dot com
----
When you are using asXML() method it also outputs start and end tag of root element.
For example :
<?php
$xml = "<text> Something <b>new</b>
</text>"
$obj = simplexml_load_string($xml);
$obj->asXML(); //returns xml text with "text" tag
?>
To remove start and end tag of asXML() result I wrote simple function that uses regular expressions.
<?php
/**
* Getting $content of formatted expression
* "<tag attributes>$content</tag>". Tag will
* automatically determined.
*
* @author QoSyS
* @return content of the tag
*/
function subXML($s){
$s = preg_replace('#[\n\r]+#', "\\n", $s);
$pattern = '#<([A-Za-z][A-Za-z0-9]*)\b[^>]*>(.*?)</\1>#i';
preg_match($pattern, $s, $matches);
//print_r($matches);
return $matches[2];
}
?>