note 19938 added to ref.domxml
| From: | sbarnum at php2 dot chek dot com | Date: | Fri, 15 Mar 2002 19:37:48 +0000 |
| Subject: | note 19938 added to ref.domxml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-27839@lists.php.net to get a copy of this message | ||
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 ;
}
--
http://www.php.net/manual/en/ref.domxml.php
http://master.php.net/manage/user-notes.php?action=edit+19938
http://master.php.net/manage/user-notes.php?action=delete+19938
http://master.php.net/manage/user-notes.php?action=reject+19938