我正在写一个文件是plan.php
。这是一个我正在写的php文件。我正在使用\n
将新行放在该文件中,但它给我保存输出。如何把新文件放在php文件中fwrite
这里是代码:
$datetime = 'Monthly';
$expiry = '2017-08-07';
$expiryin = str_replace('ly', '',$datetime);
if($expiryin == 'Month' || $expiryin == 'Year') {
$time = '+ 1'.$expiryin.'s';
} else {
$time = '+'.$expiryin.'s';
}
$expiry_date = date('Y-m-d', strtotime($time, strtotime($expiry)));
$string = createConfigString($expiry_date);
$file = 'plan.php';
$handle = fopen($file, 'w') or die('Cannot open file: '.$file);
fwrite($handle, $string);
而且功能:
function createConfigString($dates){
global $globalval;
$str = '<?php \n\n';
$configarray = array('cust_code','Auto_Renew','admin_name');
foreach($configarray as $val){
$str .= '$data["'.$val.'"] = "'.$globalval[$val].'"; \n';
}
$str .= '\n';
return $str;
}
但它给人的输出,如:
<?php .......something....\n.....\n
所以我的问题是如何把此文件中有新行。
注意:代码没有错误。我已经最小化了放在这里的代码。
更换''\ n''到' “\ n”'。有时阅读手册。你甚至可以使用'PHP_EOL'常量。 –
@u_mulder替换它后给我错误,因为我在字符串的开头使用'$'。 –
您也可以使用'PHP_EOL'替代'\ n'。 –