How to put new line in php file fwrite

纵然是瞬间 提交于 2019-12-23 06:26:17

问题


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.

Here is code:

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

And the function is:

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.


回答1:


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

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




回答2:


Replace ' with " ;-)

You can learn more about strings in PHP's manual: http://docs.php.net/manual/en/language.types.string.php




回答3:


If you use \n inside a single-quoted string ($var = '\n';), it will be just that - the litteral string \n, and not a newline. For PHP to interpret that it should in fact be a newline, you need to use doublequotes ($var = "\n";).

$var = '\n';     // The litteral string \n
$var = "\n";     // Newline
  • PHP.net on double quoted strings

  • Live demo




回答4:


\n does not work inside single quotes such as '\n'. You need to use double quotes "\n". So for your purpose, the change you need to make is:

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


来源:https://stackoverflow.com/questions/45942968/how-to-put-new-line-in-php-file-fwrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!