note 76417 modified in ref.xml by danbrown

From: Date: Wed, 03 Mar 2010 18:55:20 +0000
Subject: note 76417 modified in ref.xml by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-166621@lists.php.net to get a copy of this message
I needed this for work/personal use. Sometimes you'll have a XML string generated as one long string and no line breaks...nusoap in the case of today/work, but there are any other number of possible things that will generate these. Anyways, this simply takes a long XML string and returns an indented/line-breaked version of the string for display/readability. <?php function xmlIndent($str){ $ret = ""; $indent = 0; $indentInc = 3; $noIndent = false; while(($l = strpos($str,"<",$i))!==false){ if($l!=$r && $indent>0){ $ret .= "\n" . str_repeat(" ",$indent) . substr($str,$r,($l-$r)); } $i = $l+1; $r = strpos($str,">",$i)+1; $t = substr($str,$l,($r-$l)); if(strpos($t,"/")==1){ $indent -= $indentInc; $noIndent = true; } else if(($r-$l-strpos($t,"/"))==2 || substr($t,0,2)=="<?"){ $noIndent = true; } if($indent<0){ $indent = 0; } if($ret){ $ret .= "\n"; } $ret .= str_repeat(" ",$indent); $ret .= $t; if(!$noIndent){ $indent += $indentInc; } $noIndent = false; } $ret .= "\n"; return($ret); } ?> (...this was only tested for what i needed at work, could POSSIBLY need additions) --was-- I needed this for work/personal use. Sometimes you'll have a XML string generated as one long string and no line breaks...nusoap in the case of today/work, but there are any other number of possible things that will generate these. Anyways, this simply takes a long XML string and returns an indented/line-breaked version of the string for display/readability. <? function xmlIndent($str){ $ret = ""; $indent = 0; $indentInc = 3; $noIndent = false; while(($l = strpos($str,"<",$i))!==false){ if($l!=$r && $indent>0){ $ret .= "\n" . str_repeat(" ",$indent) . substr($str,$r,($l-$r)); } $i = $l+1; $r = strpos($str,">",$i)+1; $t = substr($str,$l,($r-$l)); if(strpos($t,"/")==1){ $indent -= $indentInc; $noIndent = true; } else if(($r-$l-strpos($t,"/"))==2 || substr($t,0,2)=="<?"){ $noIndent = true; } if($indent<0){ $indent = 0; } if($ret){ $ret .= "\n"; } $ret .= str_repeat(" ",$indent); $ret .= $t; if(!$noIndent){ $indent += $indentInc; } $noIndent = false; } $ret .= "\n"; return($ret); } ?> (...this was only tested for what i needed at work, could POSSIBLY need additions) http://php.net/manual/en/ref.xml.php

« previous php.notes (#166621) next »