如何在PHP文件fwrite中添加新行 [英] How to put new line in php file fwrite

查看:97
本文介绍了如何在PHP文件fwrite中添加新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写的文件是plan.php.这是我正在编写的php文件.我正在使用\n将新行放入该文件中,但它为我提供了保存输出.

I am writing a file that is plan.php. It is a php file that I am writing. I am using \n to put the new line in that file but it is giving me save output.

这是代码:

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

但是它给出的输出如下:

But it is giving the output like:

<?php   .......something....\n.....\n

所以我的问题是如何在该文件中放入新行.

So my question is how to put the new line in this file.

注意:代码中没有错误.我已经最小化了要放在这里的代码.

Note: There is no error in code. I have minimized the code to put here.

推荐答案

每个人都已经提到过'\n'只是两个符号字符串\n.

As everyone already mentioned '\n' is just a two symbols string \n.

您需要"\n"或php核心常量PHP_EOL:

You either need a "\n" or php core constant 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;
}

有关解释字符串中的特殊字符的更多信息

More info about interpreting special characters in strings http://php.net/manual/en/language.types.string.php#language.types.string.syntax.double

这篇关于如何在PHP文件fwrite中添加新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆