Jump to content

Recommended Posts

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  ;D

 

$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);

Link to comment
https://forums.phpfreaks.com/topic/84602-new-lines-in-a-text-file/
Share on other sites

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

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>";
}
?>

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

 

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  :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.