Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write new lines into txt file

Status
Not open for further replies.

topcat1a

Programmer
Jan 6, 2005
34
GB
Hi,

Simple question:

I have three variables, I want to write each value to a txt file. I can do this.

How do I write to the file so that each value is on its own newline?

Ta
 
Add a new-line after each variable. The new line-sympol is \n so this is what you do:

Code:
$v1 = 1;
$v2 = 23;
$v3 = -18;

$file = fopen("myfile.txt", "w");
fputs($file, "$v1[COLOR=red]\n[/color]$v2[COLOR=red]\n[/color]$v3[COLOR=red]\n[/color]");
fclose($file);

Some operating systems may require both new-line and carriage-return, in which case you put \n\r.

Regards
 
Thank you, just out of curiosity how would I add the actual "\n" to a text file if it is meant to be there. i.e not act as a new line symbol
 
Oh well,

I am trying to automatically generate an XML file and need to enter <> and / as characters into the text file, does anyone know how to do this?
 
yes.

to enter a new line character you would typically use
"\r\n" (note the other way around to that posted above).

i would add a "t" to the "w" in the fopen command which will enable php to automagically select the right terminator.

to insert a "\r" or other special character just escape it first (add another backslash).

typically i'd do
Code:
fwrite($fh, addslashes($stringtoinsert)."\r\n");
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top