如何在 php 文件 fwrite 中添加新行
How to put new line in php file fwrite
我正在写一个 plan.php
的文件。这是我正在编写的 php 文件。我正在使用 \n
将新行放入该文件,但它给了我保存输出。
代码如下:
$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 的手册中了解更多关于字符串的信息:http://docs.php.net/manual/en/language.types.string.php
如果您在单引号字符串 ($var = '\n';
) 中使用 \n
,它将只是 - 乱码字符串 \n
,而不是换行符。为了 PHP 解释它实际上应该是换行符,您需要使用双引号 ($var = "\n";
).
$var = '\n'; // The litteral string \n
$var = "\n"; // Newline
\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_EOL
:
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;
}
有关解释字符串中特殊字符的更多信息http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double
我正在写一个 plan.php
的文件。这是我正在编写的 php 文件。我正在使用 \n
将新行放入该文件,但它给了我保存输出。
代码如下:
$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 的手册中了解更多关于字符串的信息:http://docs.php.net/manual/en/language.types.string.php
如果您在单引号字符串 ($var = '\n';
) 中使用 \n
,它将只是 - 乱码字符串 \n
,而不是换行符。为了 PHP 解释它实际上应该是换行符,您需要使用双引号 ($var = "\n";
).
$var = '\n'; // The litteral string \n
$var = "\n"; // Newline
\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_EOL
:
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;
}
有关解释字符串中特殊字符的更多信息http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double