我正在寫一個文件是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'。 –