dsbowmvth16379079 2011-10-25 23:19
浏览 29
已采纳

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;
}

?>
  • 写回答

1条回答 默认 最新

  • dtbsezxw28056 2011-10-25 23:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。