adam1984 Posted June 5, 2009 Share Posted June 5, 2009 Below is the code I have asking a user for their first and last name. After they submit, it saves the input to a separate file. My code works, but my processed file doesn't look pretty. Here is where I am having the trouble. For instance, if the user types in Bruce Willis, and then my next user types in Quentin Tarantino.. my .txt file where the names are written will look like this: --------------------------- Bruce Willis Quentin Tarantino --------------------------- Ideally, I would like a line break between each name. That would make it easier for me to "count lines" of the .txt file later to identify how many users have inputted names. Here is my code if anyone is available to give me some tips on how I could achieve this. Thank you in advance!! <html> <head> </title> </title> </head> <body> <?php $first= $_POST['firstname']; $last = $_POST['lastname']; $fullname= "$first $last "; $filename="people_processed.txt"; $fp = fopen($filename, "a" ) or die("Couldn't open file"); fwrite($fp, $fullname); fclose($fp); } else { $error = "You didn't enter a required field"; } } ?> <?php echo $error; ?> <form method="post" action=""> <input type="text" name="firstname" id="firstname" /> <input type="text" name="lastname" id="lastname" /> <input type="submit" name="submit" id="submit" /> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/161017-creating-a-line-break-after-writing-to-a-file/ Share on other sites More sharing options...
roopurt18 Posted June 5, 2009 Share Posted June 5, 2009 $fullname= "$first $last\n"; Quote Link to comment https://forums.phpfreaks.com/topic/161017-creating-a-line-break-after-writing-to-a-file/#findComment-849743 Share on other sites More sharing options...
adam1984 Posted June 5, 2009 Author Share Posted June 5, 2009 roopurt18>> I tried that but i'm not seeing the result :'( Is it because my people_processed file is a .txt? When I open the file I still see each name just running into eachother... would it help if people_processed was an .html file? I also tried creating a function and storing it in a variable to try it that way, but i was having trouble with that too..... Quote Link to comment https://forums.phpfreaks.com/topic/161017-creating-a-line-break-after-writing-to-a-file/#findComment-849750 Share on other sites More sharing options...
roopurt18 Posted June 5, 2009 Share Posted June 5, 2009 Open the file in Wordpad instead of Notepad. http://www.php.net/manual/en/function.fopen.php For the explanation why, ead the note that starts: Note: Different operating system families have different line-ending conventions. Notepad isn't smart enough to put "\n" by itself on a newline. It requires "\r\n". Quote Link to comment https://forums.phpfreaks.com/topic/161017-creating-a-line-break-after-writing-to-a-file/#findComment-849753 Share on other sites More sharing options...
adam1984 Posted June 5, 2009 Author Share Posted June 5, 2009 Haha, you are awesome. Thank you. I have tried the \n and have been working on this for hours. Very much appreciate it. Am I going to find wordpad more useful in the future as well? Quote Link to comment https://forums.phpfreaks.com/topic/161017-creating-a-line-break-after-writing-to-a-file/#findComment-849755 Share on other sites More sharing options...
roopurt18 Posted June 5, 2009 Share Posted June 5, 2009 Only if you intend to keep using "\n" as your newline character and want to view it on windows. You just have to be mindful of which OS this text file will be viewed on. Most text editing programs are smart enough to treat \n, \r, and \r\n the same, meaning they cause a carriage return linefeed. Notepad is the only text editor I'm aware of that doesn't know what to do with them. So if you're generating text files that will be e-mailed to non-tech savvy users, then you should use \r\n. Otherwise \n is typically preferred. Quote Link to comment https://forums.phpfreaks.com/topic/161017-creating-a-line-break-after-writing-to-a-file/#findComment-849758 Share on other sites More sharing options...
adam1984 Posted June 5, 2009 Author Share Posted June 5, 2009 Thanks for clearing that up! Quote Link to comment https://forums.phpfreaks.com/topic/161017-creating-a-line-break-after-writing-to-a-file/#findComment-849762 Share on other sites More sharing options...
Daniel0 Posted June 5, 2009 Share Posted June 5, 2009 There is a built-in constant called PHP_EOL that will contain the line break sequence for your operating system. Quote Link to comment https://forums.phpfreaks.com/topic/161017-creating-a-line-break-after-writing-to-a-file/#findComment-849769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.