PHP fwrite new line
Use PHP_EOL
which produces \r\n
or \n
$data = 'my data' . PHP_EOL . 'my data';
$fp = fopen('my_file', 'a');
fwrite($fp, $data);
fclose($fp);
// File output
my data
my data
How to put new line in php file fwrite
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
Making new line in php write to file
It is the best to use PHP_EOL. It's cross-platform and will automatically choose the correct newline character(s) for the platform PHP is running on.
$txt = "Goofy".PHP_EOL;
PHP fwrite() how to insert a new line after some specific line
I modified your code, I think follows is what you need, and I also put comment, function below will keep adding new user, you can add condition that to check user exist.
$config = 'test.txt';
$file=fopen($config,"r+") or exit("Unable to open file!");
$date = date("F j, Y");
$time = date("H:i:s");
$username = "user";
$password = "pass";
$email = "email";
$newuser = $username . " " . $password . " " . $email . " " . $date . " " . $time."\r\n"; // I added new line after new user
$insertPos=0; // variable for saving //Users position
while (!feof($file)) {
$line=fgets($file);
if (strpos($line, '//Users')!==false) {
$insertPos=ftell($file); // ftell will tell the position where the pointer moved, here is the new line after //Users.
$newline = $newuser;
} else {
$newline.=$line; // append existing data with new data of user
}
}
fseek($file,$insertPos); // move pointer to the file position where we saved above
fwrite($file, $newline);
fclose($file);
PHP fwrite function new line
Depending on your system, your lines may not end with a \r\n combination, but perhaps just \r.
I suggest either change str_replace to:
$textAr[$i] = str_replace(array("\r","\n"),"", $textAr[$i]);
Or, change the array:
$finalArray = array(trim($textAr[$i]), $m1, $m2, $m3, $m4, $m5, $m6);
Incidentally, although it will work, your implode parameters are reversed:
$test = implode("***", $finalArray);
fwrite - need a new line after variable - PHP
What you need to use is \r\n
.
Writing a new line to file in PHP (line feed)
Replace '\n'
with "\n"
. The escape sequence is not recognized when you use '
.
See the manual.
For the question of how to write line endings, see the note here. Basically, different operating systems have different conventions for line endings. Windows uses "\r\n", unix based operating systems use "\n". You should stick to one convention (I'd chose "\n") and open your file in binary mode (fopen
should get "wb", not "w").
Related Topics
PHP Get Previous Array Element Knowing Current Array Key
Laravel - How to Get Current User in Appserviceprovider
Can't Execute PHP Script Using PHP Exec
How to Prevent PHP Files from Being Downloaded? and What Are Some Ways Someone Can Download Them
PHP Get the Last 3 Elements of an Associative Array While Preserving the Keys
How to Get Info on Sent PHP Curl Request
How to Detect Non-Ascii Characters in a String
Reaching 100% Code Coverage with PHPunit
Codeigniter Model Error: Undefined Property
Making a Http Get Request with Http-Basic Authentication
Best Way to Autoload Classes in PHP