note 98226 added to simplexmlelement.asXML
| From: | dimzav at gmail dot com | Date: | Wed, 02 Jun 2010 20:43:03 +0000 |
| Subject: | note 98226 added to simplexmlelement.asXML | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-169832@lists.php.net to get a copy of this message | ||
Fixed bug in gary dot malcolm at gmail dot com asPrettyXml() function:
/**
* Pretty format xml.
*
* @static
* @link http://php.net/manual/en/simplexmlelement.asXML.php#77816
* @param string $string
* @return string
*/
private static function _prettyFormatXML($string) {
// put each element on it's own line
$string = preg_replace("#>\s*<#", ">\n<", $string);
// each element to own array
$xmlArray = explode("\n", $string);
// holds indentation
$currIndent = 0;
// set xml element first by shifting of initial element
$string = array_shift($xmlArray) . "\n";
foreach ($xmlArray as $element) {
$element = trim($element, "\n");
// find open only tags... add name to stack, and print to string increment $currIndent
if (preg_match('#^<([\w])+[^>]*>$#U', $element)) {
$string .= str_repeat(' ', $currIndent) . $element . "\n";
$currIndent += self::INDENT;
} elseif (preg_match('#^<\/.+>$#', $element)) {
// find standalone closures, decrement $currIndent, print to string
$currIndent -= self::INDENT;
if ($currIndent < 0) {
echo $element."\n".$string;
exit();
}
$string .= str_repeat(' ', $currIndent) . $element . "\n";
} else {
// find open/closed tags on the same line print to string
$string .= str_repeat(' ', $currIndent) . $element . "\n";
}
}
return $string;
}
----
Server IP: 69.147.83.197
Probable Submitter: 94.178.172.70
----
Manual Page -- http://www.php.net/manual/en/simplexmlelement.asXML.php
Edit -- https://master.php.net/note/edit/98226
Del: integrated -- https://master.php.net/note/delete/98226/integrated
Del: useless -- https://master.php.net/note/delete/98226/useless
Del: bad code -- https://master.php.net/note/delete/98226/bad+code
Del: spam -- https://master.php.net/note/delete/98226/spam
Del: non-english -- https://master.php.net/note/delete/98226/non-english
Del: in docs -- https://master.php.net/note/delete/98226/in+docs
Del: other reasons-- https://master.php.net/note/delete/98226
Reject -- https://master.php.net/note/reject/98226
Search -- https://master.php.net/manage/user-notes.php