如何在php文件fwrite中放入新行
我正在编写一个名为plan.php的文件。这是我正在编写的一个php文件。我正在使用\n将新行放入该文件中,但它提供了保存输出 以下是代码:如何在php文件fwrite中放入新行,php,fwrite,file-writing,Php,Fwrite,File Writing,我正在编写一个名为plan.php的文件。这是我正在编写的一个php文件。我正在使用\n将新行放入该文件中,但它提供了保存输出 以下是代码: $datetime = 'Monthly'; $expiry = '2017-08-07'; $expiryin = str_replace('ly', '',$datetime); if($expiryin == 'Month' || $expiryin == 'Year') { $time = '+ 1'.$expiryin.'s'; } el
$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
所以我的问题是如何在这个文件中添加新行
注意:代码中没有错误。我已经最小化了要放在这里的代码。替换为-
您可以在PHP手册中了解有关字符串的更多信息:如果在单个带引号的字符串$var='\n';,它将只是一个普通字符串,而不是换行符。为了让PHP解释它实际上应该是一个换行符,您需要使用双引号$var=\n
\n在诸如'\n'这样的单引号内不起作用。您需要使用双引号\n。因此,出于您的目的,您需要进行的更改是:
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"; // change places of double and single quotes
}
$str .= "\n"; // change places of double and single quotes
return $str;
}
正如大家已经提到的,“\n”只是一个两个符号的字符串\n 您需要\n或php核心常量php\u EOL:
有关解释字符串中特殊字符的详细信息将“\n”替换为\n。有时也读一本手册。您甚至可以使用PHP_EOL常量。@u_mulder在替换它后会给我带来错误,因为我在字符串开头使用了$。您也可以使用PHP_EOL作为替代,而不是\n。认真地说,有了这样的代表,您可以想到类似“;”的东西\n@这是sugarcrm配置修改。所以我不能再做更多的改变了。
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"; // change places of double and single quotes
}
$str .= "\n"; // change places of double and single quotes
return $str;
}
function createConfigString($dates){
global $globalval;
// change here
$str = '<?php ' . PHP_EOL . PHP_EOL;
$configarray = array('cust_code','Auto_Renew','admin_name');
foreach($configarray as $val){
// change here
$str .= '$data["'.$val.'"] = "'.$globalval[$val].'";' . PHP_EOL;
}
// change here
$str .= PHP_EOL;
return $str;
}