note 19938 deleted from ref.domxml by sniper
| From: | sniper@php.net | Date: | Mon, 03 Nov 2003 15:47:52 +0000 |
| Subject: | note 19938 deleted from ref.domxml by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-59713@lists.php.net to get a copy of this message | ||
Note Submitter: sbarnum at pointsystems com
----
Here's a nice function for formatting an xml document. Adds indents and line breaks.
-----------------------------------------------------
function xml_format($xml) {
// v 1.1, escapes backslash brackets //
$xml = str_replace ("\n", "", $xml);
$xml = str_replace ("\r", "", $xml);
$xml = str_replace ("\\>", ">", $xml);
$xml = str_replace ("\\<", "<", $xml);
$out = preg_replace_callback("|\<[^\>]*\>[^\<]*|",
"_xml_format_element", $xml);
$out = str_replace ('>', '>', $out);
$out = str_replace ('<', '<', $out);
$out = str_replace ('<', '<', $out);
$out = str_replace ('>', '>', $out);
return nl2br($out);
}
function _xml_format_element($array) {
// callback function for xml_format. Do not invoke directly //
$found = $array[0];
static $indent;
$found = trim($found);
$tagOffset = 1;
$openingText = "\n";
$tab = str_repeat(" ", $indent * 4);
if (substr($found, 0, 2)=='</') {
// closing tag //
$tagOffset = 2;
$indent--;
$tab = str_repeat(" ", $indent *4);
} else if (substr($found, -2, 1)=='/' || strpos($found, '/>')) {
// opening and closing tag //
// do not change indent //
} else {
// opening tag //
$indent++;
}
// $content = substr(strrchr($found, '>'), 0, -1);
if (substr($found, -1) != '>') {
// indent the content //
$found = str_replace (">", ">\n" .
str_repeat(" ", ($indent+0)*4), $found);
}
return $openingText . $tab . $found ;
}