RE: [PHP] LIBXML- Auto-Indent - How do it?
| From: | Christian Stocker | Date: | Mon, 17 Dec 2001 13:56:37 +0000 |
| Subject: | RE: [PHP] LIBXML- Auto-Indent - How do it? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-77932@lists.php.net to get a copy of this message | ||
In <100984FEB15DD511BA9300104B6980760AC36B@EXCHANGE-RIO>, Ricardo Junior
wrote:
> The conversion to plain text mail format changed the correct indent that
> I need.... I'm needing it like this:
>
> <ROOT>
> <LEVEL-1>
> <LEVEL2/>
> </LEVEL-1>
> </ROOT>
>
> Thanks !!!
no need to scream here :)
here's a function, which maybe helps you (it adds indentation to a string
containing xml-stuff)
function get_indented_xml($xml,$instring = " ") {
$xml = preg_replace("/(\>)\n/","$1",$xml);
$xml = preg_replace("/\>\s*\</",">\n<",$xml);
$axml = explode("\n",$xml);
$indent=-1;
$xmls = "";
foreach ($axml as $key => $value) {
if (preg_match("/<[^\/{1}]/",$value)) {
$indent++;
}
if ($indent < 0)
$indent = 0;
$xmls .= str_repeat($instring,$indent);
if (preg_match("/\<\//",$value) ||
preg_match("/\/\>/",$value)|| preg_match("/-->/",$value)) {
$indent--;
}
$xmls .= trim($value)."\n";
}
return $xmls;
}