note 91842 modified in function.simplexml-element-asXML by danbrown
| From: | danbrown@php.net | Date: | Sun, 28 Jun 2009 14:51:09 +0000 |
| Subject: | note 91842 modified in function.simplexml-element-asXML by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157644@lists.php.net to get a copy of this message | ||
Getting the subtree without the start and end tags:
<?php
$string = <<<XML
<a>
<b>
<c>text <b>hello</b><i>hello</i></c>
<c>stuff</c>
</b>
<d>
<c>hella</c>
</d>
</a>
XML;
//=================================
function subTree( $s , $tag ){
if(substr($s,0,1+strlen($tag))!='<'.$tag) return false;
$news='';
for($i=1+strlen($tag);$i<strlen($s);$i++){
if(substr($s,$i,1) == '>') break;
}
$news=substr($s,$i+1);
if( substr($news,strlen($news)-strlen($tag)-3) !='</'.$tag.'>') return
false;
return(substr($news,0,strlen($news)-strlen($tag)-3));
}
//=================================
$xml = new SimpleXMLElement($string);
echo '<textarea rows="10" cols="40">' . $xml->asXML() .
'</textarea><br />';
echo '<textarea rows="6" cols="40">' . $xml->b->asXML() .
'</textarea><br />' ;
echo '<textarea rows="6" cols="40">' . subTree(
$xml->b->asXML() , 'b' ) . '</textarea><br />' ;
?>
--was--
Getting the subtree without the start and end tags:
$string = <<<XML
<a>
<b>
<c>text <b>hello</b><i>hello</i></c>
<c>stuff</c>
</b>
<d>
<c>hella</c>
</d>
</a>
XML;
//=================================
function subTree( $s , $tag ){
if(substr($s,0,1+strlen($tag))!='<'.$tag) return false;
$news='';
for($i=1+strlen($tag);$i<strlen($s);$i++){
if(substr($s,$i,1) == '>') break;
}
$news=substr($s,$i+1);
if( substr($news,strlen($news)-strlen($tag)-3) !='</'.$tag.'>') return
false;
return(substr($news,0,strlen($news)-strlen($tag)-3));
}
//=================================
$xml = new SimpleXMLElement($string);
echo '<textarea rows="10" cols="40">' . $xml->asXML() .
'</textarea><br />';
echo '<textarea rows="6" cols="40">' . $xml->b->asXML() .
'</textarea><br />' ;
echo '<textarea rows="6" cols="40">' . subTree(
$xml->b->asXML() , 'b' ) . '</textarea><br />' ;
http://php.net/manual/en/function.simplexml-element-asXML.php