note 96618 added to simplexmlelement.asXML
| From: | qosys dot ney at gmail dot com | Date: | Mon, 08 Mar 2010 18:53:54 +0000 |
| Subject: | note 96618 added to simplexmlelement.asXML | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-166793@lists.php.net to get a copy of this message | ||
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];
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 91.78.81.169
----
Manual Page -- http://www.php.net/manual/en/simplexmlelement.asXML.php
Edit -- https://master.php.net/note/edit/96618
Del: integrated -- https://master.php.net/note/delete/96618/integrated
Del: useless -- https://master.php.net/note/delete/96618/useless
Del: bad code -- https://master.php.net/note/delete/96618/bad+code
Del: spam -- https://master.php.net/note/delete/96618/spam
Del: non-english -- https://master.php.net/note/delete/96618/non-english
Del: in docs -- https://master.php.net/note/delete/96618/in+docs
Del: other reasons-- https://master.php.net/note/delete/96618
Reject -- https://master.php.net/note/reject/96618
Search -- https://master.php.net/manage/user-notes.php