Aug 23, 2006 #1 topcat1a Programmer Joined Jan 6, 2005 Messages 34 Location 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
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
Aug 23, 2006 #2 dkdude Programmer Joined Jun 16, 2003 Messages 849 Location DK 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 Upvote 0 Downvote
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
Aug 23, 2006 Thread starter #3 topcat1a Programmer Joined Jan 6, 2005 Messages 34 Location GB 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 Upvote 0 Downvote
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
Aug 23, 2006 Thread starter #4 topcat1a Programmer Joined Jan 6, 2005 Messages 34 Location GB 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? Upvote 0 Downvote
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?
Aug 23, 2006 #5 jpadie Technical User Joined Nov 24, 2003 Messages 10,094 Location FR 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"); Upvote 0 Downvote
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");
Aug 23, 2006 Thread starter #6 topcat1a Programmer Joined Jan 6, 2005 Messages 34 Location GB thanks for the help Upvote 0 Downvote