Jump to content

[SOLVED] A Line Break Thread


Zeradin

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/116292-solved-a-line-break-thread/
Share on other sites

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.

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?

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.