gravereaper Posted November 30, 2006 Share Posted November 30, 2006 $myFile = "testFile.txt";$fh = fopen($myFile, 'w') or die("can't open file");$stringData = "Bobby Bopper\n";fwrite($fh, $stringData);$stringData = "Tracy Tanner\n";fwrite($fh, $stringData);fclose($fh); This code should simply write the two names on different lines but instead of a line break its displaying a character normally used a symbol for line break. How can i read this txt file so that character is considered as an actual line break and the two names appear on different lines? Link to comment Share on other sites More sharing options...
jesh Posted November 30, 2006 Share Posted November 30, 2006 You might try "\n\r" as the new line code. Link to comment Share on other sites More sharing options...
gravereaper Posted November 30, 2006 Author Share Posted November 30, 2006 Its still displaying the same results Link to comment Share on other sites More sharing options...
justsomeguy Posted November 30, 2006 Share Posted November 30, 2006 Try using this to open it:$fh = fopen($myFile, 'wb') or die("can't open file"); Link to comment Share on other sites More sharing options...
gravereaper Posted November 30, 2006 Author Share Posted November 30, 2006 Still the same thing, one thing i'll like to add is that whenever i try to copy and paste that text with those symbols here, the text automatically appears on separate lines. Any hints on how to read it? Link to comment Share on other sites More sharing options...
justsomeguy Posted November 30, 2006 Share Posted November 30, 2006 What are you looking at the text file in? Are you opening it in Notepad, or something else? If you open it in more then one program, do they all show the same thing? You should be able to attach the text file as an attachment to your post. Link to comment Share on other sites More sharing options...
gravereaper Posted December 1, 2006 Author Share Posted December 1, 2006 Ok those symbols were only appearing in NOTEPAD, the text was on separate lines when i tried opening .txt file in wordpad. Link to comment Share on other sites More sharing options...
vijay Posted December 1, 2006 Share Posted December 1, 2006 Hi,Plz try with like this: this will probably work$data="Content"."\n";Regards,Vijay Link to comment Share on other sites More sharing options...
gravereaper Posted December 1, 2006 Author Share Posted December 1, 2006 Its writing in the .txt file fine now, how can i read all the contents of the file from the first to the last line? Link to comment Share on other sites More sharing options...
aspnetguy Posted December 1, 2006 Share Posted December 1, 2006 http://www.free2code.net/plugins/articles/read.php?id=84 Link to comment Share on other sites More sharing options...
justsomeguy Posted December 1, 2006 Share Posted December 1, 2006 http://www.php.net/manual/en/function.file-get-contents.php Link to comment Share on other sites More sharing options...
gravereaper Posted December 2, 2006 Author Share Posted December 2, 2006 I'am putting the codes right but there seems to be a problem.. i'am using the following codes to write to the file: <?php$myFile = "testfile.txt";$fh = fopen($myFile, 'w');$datastring = "text 1\n";fwrite($fh,$datastring);$datastring = "text2\n";fwrite($fh,$datastring);fclose($fh);?> and the following to read it: <?php$myFile = "testfile.txt";$fh = fopen($myFile, 'r');$theData = fread($fh, filesize($myFile));fclose($fh);echo $theData;?> But instead of separate lines, its displaying the file contents in the same line, thats what i was trying to figure out.. Link to comment Share on other sites More sharing options...
jlhaslip Posted December 2, 2006 Share Posted December 2, 2006 replace \n with \r\n when you write to the file. Link to comment Share on other sites More sharing options...
gravereaper Posted December 2, 2006 Author Share Posted December 2, 2006 Ok now its appearing in every Text Reader but still when i try to read it through the above script its showing everything in a horizontal lineContents of the actual .txt file:text 1text2When read in the browser:text 1 text2 Link to comment Share on other sites More sharing options...
jlhaslip Posted December 2, 2006 Share Posted December 2, 2006 The \r\n is for the text file only. Add a <br /> tag to the data in the file or in your echo statements to have the Browser output behave as you wish. The read function you are using reads the entire file contents. Maybe switch to a function which reads single lines at a time and add the br tag on each loop as you read / write the file contents. Check at the php site for the different functions available. fgets() looks promissing. http://us3.php.net/manual/en/function.fgets.php Link to comment Share on other sites More sharing options...
justsomeguy Posted December 4, 2006 Share Posted December 4, 2006 You can just use the nl2br function to add a <br /> either before or after every newline in the file.echo nl2br(file_get_contents("file.txt")); Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now