PHP在IE Vista上保存文件,添加换行符

I tried this to format the XML output in a PHP function with the formatoutput = true and that didn't do it. So I want to do this with a function. I found two different scripts for that but they all have the same issue: they do the indentation but the newline " " doesn't print in the file. Is there a different way to get the newline?

PHP script

<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

function make_update( $nodeid, $name, $top, $left, $width, $height ) {

$nodes = new SimpleXMLElement('linkcards.xml', null, true);

$returnArray = $nodes->xpath("//LINKCARD[@ID='$nodeid']");  
$node = $returnArray[0]; 
$node->NAME = $name;
$node->TOP = $top;
$node->LEFT = $left;
$node->WIDTH = $width;
$node->HEIGHT = $height;

$nodes->asXML('linkcards.xml');

$formatted = formatXmlString($nodes->asXML());

$file = fopen ('linkcards.xml', "w"); 
fwrite($file, $formatted); 
fclose ($file);  

}

echo make_update(trim($_REQUEST['nodeid']),trim($_REQUEST['name']),trim($_REQUEST['top']),trim($_REQUEST['left']),trim($_REQUEST['width']),trim($_REQUEST['height']));

function formatXmlString($xml) {  

  // add marker linefeeds to aid the pretty-tokeniser (adds a linefeed between all tag-end boundaries)
  $xml = preg_replace('/(>)(<)(\/*)/', "$1
$2$3", $xml);

  // now indent the tags
  $token      = strtok($xml, "
");
  $result     = ''; // holds formatted version as it is built
  $pad        = 0; // initial indent
  $matches    = array(); // returns from preg_matches()

  // scan each line and adjust indent based on opening/closing tags
  while ($token !== false) : 

    // test for the various tag states

    // 1. open and closing tags on same line - no change
    if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) : 
      $indent=0;
    // 2. closing tag - outdent now
    elseif (preg_match('/^<\/\w/', $token, $matches)) :
      $pad--;
    // 3. opening tag - don't pad this one, only subsequent tags
    elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) :
      $indent=1;
    // 4. no indentation needed
    else :
      $indent = 0; 
    endif;

    // pad the line with the required number of leading spaces
    $line    = str_pad($token, strlen($token)+$pad, ' ', STR_PAD_LEFT);
    $result .= $line . "
"; // add to the cumulative result, with linefeed
    $token   = strtok("
"); // get the next token
    $pad    += $indent; // update the pad size for subsequent lines    
  endwhile; 

  return $result;
}

?>

Found the answer. If I write " " for the newline, it works!!!!!!

$result .= $line . "
"; 

Using that function instead of formatoutput = true might be a useful workaround for anyone else who had trouble formatting the XML output.