t0mmy9 Posted January 5, 2008 Share Posted January 5, 2008 hi i have a php script thats saving the contents of a textarea to a text file. the textarea is called "message" now heres the problem: when someone presses enter to go to a new line in the textarea i want it to write to the text file a br instead of going to a new line. i tried nl2br $stringData = nl2br($_POST['message']); but this doesnt work either. my code is below. thanks for any help $myFile = "message.txt"; $fh = fopen($myFile, 'a'); $stringData = "\n"; fwrite($fh, $stringData); $stringData = $txtid; fwrite($fh, $stringData); $stringData = nl2br($_POST['message']); fwrite($fh, $stringData); fclose($fh); Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/ Share on other sites More sharing options...
trq Posted January 5, 2008 Share Posted January 5, 2008 Why would you want to do that? Its better to store your data in its raw format. Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/#findComment-431105 Share on other sites More sharing options...
t0mmy9 Posted January 5, 2008 Author Share Posted January 5, 2008 because im making a shoutbox and i have more code that reverses the text making the newest entries first. but pressing enter in the text area messes this up Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/#findComment-431106 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2008 Share Posted January 5, 2008 Create an array from the lines entered, write the serialized array to your file. When displaying, unserialize the line, and use the implode() function to put in the linebreaks. <?php $myFile = "message.txt"; $fh = fopen($myFile, 'a'); fwrite($fh, "\n$txtid\n"); $stringData = nl2br($_POST['message']); fwrite($fh, serialize(explode("\n",$_POST['message']))."\n"); fclose($fh); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/#findComment-431115 Share on other sites More sharing options...
t0mmy9 Posted January 5, 2008 Author Share Posted January 5, 2008 thanks, when i entered this is a test into the text box i got a:2:{i:0;s:8:"this is ";i:1;s:6:"a test";} ill try and see what ive done wrong. this is the php that reverses it by the way <?php $text = file('message.txt'); $text= array_reverse($text); foreach($text as $line) { echo $line , "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/#findComment-431117 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2008 Share Posted January 5, 2008 That is what the serialized array looks like. When you read it back, unserialize it and display it: <?php $text = file('message.txt'); $text= array_reverse($text); foreach($text as $line) { echo implode('<br>',unserialize($line)) . "<br>"; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/#findComment-431121 Share on other sites More sharing options...
t0mmy9 Posted January 5, 2008 Author Share Posted January 5, 2008 that seems to have done the job, thanks. $txtid contains html code, can you not use html with this method? Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/#findComment-431124 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2008 Share Posted January 5, 2008 I have to leave for a few hours ... I'll post a solution when i come back. Ken Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/#findComment-431125 Share on other sites More sharing options...
t0mmy9 Posted January 5, 2008 Author Share Posted January 5, 2008 ok no problem, thanks for your help edit:solved using this code: $myFile = "message.txt"; $fh = fopen($myFile, 'a'); fwrite($fh, serialize(explode("\n",$_POST['message']))."\n"); fwrite($fh, serialize(explode("\n",$txtid))."\n"); fclose($fh); the only problem was i wanted $txtid to be on the same line as the message but that doesnt really matter as long as this works. thanks again for your help Quote Link to comment https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/#findComment-431126 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.