Zeradin Posted July 23, 2008 Share Posted July 23, 2008 I just have a hard time visualizing where this will go. I have an html form: <form method="POST" action="newstory.php"> <b>Message</b><br /> <textarea rows="30" name="message" cols="80"></textarea> <br /> <br /> <input type="submit" value="Submit" name="newssubmit"> </form> and then in newstory.php I have <?php if(isset($_POST['newssubmit'])) { $news = $_POST['message']; } $file = "generalnews.txt"; $fh = fopen($file, 'a') or die('Could not open file!'); fwrite($fh, "$news\n") or die('Could not write to file'); fclose($fh); so it writes into the file with breaks then i read it out in another php file // set file to read $file = 'generalnews.txt' or die('Could not read file!'); $fh = fopen($file, 'r'); // read file into array $data_array = fread($fh, filesize($file)) or die('Could not read file!'); fclose($fh); I don't think I'm missing any pertinent parts of that. Anyway... where do I convert the line breaks to html line breaks? and how do I do it? I'd imagine it needs to be written to the text file with them, but then again maybe the php can see the breaks in the text file and convert it that way. You guys always come through so thanks in advance. Quote Link to comment Share on other sites More sharing options...
Zeradin Posted July 23, 2008 Author Share Posted July 23, 2008 I should point out that it reads multiple lines out of the text file correctly because i exploded the text into parts... I just tried to simplify the code... maybe a mistake. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 23, 2008 Share Posted July 23, 2008 You want to convert them to break line tags when you output the data. As a general rule, you should preserve data how it was written and format it for presentation at the point that you present it. As for how to do it, use the nl2br() function. Quote Link to comment Share on other sites More sharing options...
Zeradin Posted July 24, 2008 Author Share Posted July 24, 2008 How can I preserve the data I'm reading it in like $file = 'generalnews.txt' or die('Could not read file!'); $fh = fopen($file, 'r'); // read file into array $data_array = fread($fh, filesize($file)) or die('Could not read file!'); fclose($fh); $data = explode("'[END]'", $data_array); so the last part of it could be multiple lines and the only way I knew how to read that was to break it up using the '[END]' as someone recommended here. I'm pretty sure that just reads it all as a string. Is there any other way to do that? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 24, 2008 Share Posted July 24, 2008 I'm not sure if I follow your question, but you're always going to need some sort of delimiter if you wish to store data in a text file. What you choose to use is up to you; though you must remember to ensure that it cannot occur in the input. Quote Link to comment Share on other sites More sharing options...
Zeradin Posted July 24, 2008 Author Share Posted July 24, 2008 Yeah I have a delimiter '[END]' that breaks it up into parts, but when it reads in for example: title'[END]' date'[END]' message with line breaks'[END]' with // set file to read $file = 'generalnews.txt' or die('Could not read file!'); $fh = fopen($file, 'r'); // read file into array $data_array = fread($fh, filesize($file)) or die('Could not read file!'); fclose($fh); // count the number of lines and divide it into number of news stories $lines = count($data); $data = explode("'[END]'", $data_array); ?> it lists the message with line breaks as just a big string, no? so I figure I have to put the html line breaks into the text file so it reads it out of there. Or is there another way to read out the data into the array not as a string? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 24, 2008 Share Posted July 24, 2008 As i said earlier, you can convert new lines to break line tags with the nl2br() function. You should do this just before you echo the data. Quote Link to comment Share on other sites More sharing options...
Zeradin Posted July 24, 2008 Author Share Posted July 24, 2008 oh F#@$ sorry i just didn't understand how the function worked. I was using it outside of the echo trying to convert the variable to a new one and then echo it, once i put it after the echo it worked perfectly. Thanks so much... sorry for being such a pain. Quote Link to comment 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.