2011-10-25 8 views
0

これを使って、formatoutput = trueのPHP関数でXML出力をフォーマットしようとしましたが、それはしませんでした。だから私は関数でこれをしたい。私は2つの異なるスクリプトを見つけましたが、それらはすべて同じ問題があります。インデントを行いますが、改行 "\ n"はファイルには印刷されません。改行を取得する別の方法はありますか?PHPのファイルをIE Vistaに保存して改行を追加する

<?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\n$2$3", $xml); 

    // now indent the tags 
    $token  = strtok($xml, "\n"); 
    $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 . "\n"; // add to the cumulative result, with linefeed 
    $token = strtok("\n"); // get the next token 
    $pad += $indent; // update the pad size for subsequent lines  
    endwhile; 

    return $result; 
} 

?> 

答えて

0

PHPスクリプトは答えを見つけました。改行のために "\ r \ n"と書くと、それは動作します!!!!!!

$result .= $line . "\r\n"; 

formatoutput = trueではなくその関数を使用すると、XML出力の書式設定に問題がある他の人にとって便利な回避策になる可能性があります。

関連する問題