The above function formatXmlString is a great job ! Thanks!
I updated it so one can use any symbol for indentation and also encode tags for displaying the code on a web page. Here's the new code and an example.
Just replace these lines into the above code
function formatXmlString( $xml = "", $indent_symbol = " ", $crlf = "\n", $bool_enc_tags = 0 )
$line = str_pad($token, strlen($token)+$pad * $indent_symbol_len, $indent_symbol, STR_PAD_LEFT);
After the while-loop, put the following
if ( $bool_enc_tags )
{
$result = str_replace( "<", "<", $result );
$result = str_replace( ">", ">", $result );
}
if ( $crlf != "\n" )
$result = preg_replace( "/\n/", $crlf, $result );
return $result;
Then test it as below
echo formatXmlString( '<root><foo><bar>baz</bar></foo></root>', str_repeat( " ", 4 ), "<br/>", true );