Jump to content

Reading A Text File


CanadianGuy

Recommended Posts

Hi Everyone,I've been reading way too much to resolve this issue so I turn you because I just can not figure out what I am missing.I have a php form with a text box that when submitted passing the content to a script file that saves a text file. I can then load the text file into a read only file with a text box and all is well. Problem is I don't want the text box and when I eliminate it I loose my line feeds.The form...

<html><body><form action="processscript.php" method="post"><textarea rows="25" cols="40" name="content"><?php$fn = "test.txt";print htmlspecialchars(implode("",file($fn)));?></textarea><br><input type="submit" value="Change!"> </form></body></html>

The processor...

<?php$fn = "test.txt";$content = stripslashes($_POST['content']);$fp = fopen($fn,'w') or die ("Error opening file in write mode!");fputs($fp,$content);fclose($fp) or die ("Error closing file!");echo "<meta http-equiv=\"refresh\" content=\"0; url=output.php\" />\n";?>

The working read file with the text box...

<html><body><textarea rows="25" cols="40" name="content"><?php$fn = "test.txt";print htmlspecialchars(implode("",file($fn)));?></textarea><br></form><a href="form.php">- Edit -</a> <a href="output.php">- Output One -</a></body></html>

The last file is what I am going for but this is the file that does read the text file but looses line returns

<html><body><?php$fn = "test.txt";print htmlspecialchars(implode("",file($fn)));?><a href="form.php">- Edit -</a> <a href="output2.php">- Output Two -</a></body></html>

The obvious thing I can see is that in my last file I loose the "content" name but I can not figure out how to get around this. Any suggestions you have are always appreciated.Thanks!

Link to comment
Share on other sites

Thank you justanotherguy, I appreciate that you are trying to help me. But I’m not getting very far with your explanation. So I will go into details. Hold on, here it comes.Perhaps vagueness is a technique employed to help folks help themselves but in this case it really is not helping. Learning php for real life applications has been less then rewarding because there seems to be so little real world examples of this code.Less then a week ago I wrote my first php file…

<?php  echo "Hello World!";?>

Was this rewarding? Nope. Considering I could accomplish the same in html without any interest in php whatsoever. So after another 12 hours of my life googling nl2br and finding nothing more helpful then….

<?phpecho nl2br("One line.\nAnother line.");?>

How does the above code really help me understand the use of this code? Well it doesn’t help me in the least (but that's just me). This is yet again just another example of code that I could accomplish with html with absolutely no reason to use php in the first place.OK so I move on. I take your vague explanation, add it to the less then real world example of the use of this code and I attack the code that some how I’ve managed to get this far with.I add it here because it seems to make sense but it doesn’t work. So I try it here… Nope, that does work either. Here, nope, there, nope, here and there, nope that doesn’t work either.Eureka! Maybe I need to apply this code not to my read only file but perhaps I need to use it in my processing file to write <br /> to my text file. Add code here, nope. And here and there and here and there and……. You get the idea.So if you (or any one else) could edit my code to include the use of nl2br it would help accomplish my goal and likely I would learn slightly from a working example. Now, if you could edit my code to include the use of nl2br AND explain why it belongs where it does and why it works, then I have truly learned.If I were 18 years old I would go to college and learn this with real world examples. But my reality is that I am pushing 50 and have cancer. I’ve been building a website for my family to keep in touch. Life is too short for me to be throwing darts at the board.If you can help me with my code and make it work, thank you! If you can help me with my code and explain why, thank you very much.I sincerely appreciate your help, but regrettably I’ve failed to learn from your efforts thus far. If you could be of further assistance I would be truly grateful.

Link to comment
Share on other sites

file() returns an array of lines in your document, using the newline as a delimiter.implode() returns a string of glued-together array elements. You're gluing them with "". So implode is effectively putting an empty string where your newline characters are. Try changing that empty string to "<br />"; e.g. :print htmlspecialchars(implode("<br />", file($fn) ) );This will glue your array elements (lines) with the "<br />" element.I assume your intention is to make the "<br />" string visible in your textarea, like the textarea is being used as an HTML editor?

Link to comment
Share on other sites

file() returns an array of lines in your document, using the newline as a delimiter.implode() returns a string of glued-together array elements. You're gluing them with "". So implode is effectively putting an empty string where your newline characters are. Try changing that empty string to "<br />"; e.g. :print htmlspecialchars(implode("<br />", file($fn) ) );This will glue your array elements (lines) with the "<br />" element.I assume your intention is to make the "<br />" string visible in your textarea, like the textarea is being used as an HTML editor?
Hmmm… nope, I’m not even going to pretend that I understand this. :)No this is not a HTML editor, just a text editor. I’m simply after allowing my family to log in to my site, and change their comments.So for example my sister goes to file 1 (the editor) and types…Paragraph oneParagraph twoParagraph three…Hits the update button and file 2 (the processor) saves her comments in a text file.Now if we view her comments in file 3 (from my above examples) that includes a text box all is well and her comments appear just fine within the text box. But if we view her comments in file 4 (without the text box) it appears as…Paragraph one Paragraph two Paragraph threeUsing your example I get…Paragraph one <br /> <br /> Paragraph two <br /> <br /> Paragraph threeThe breaks in the above line is not what actually happens but rather that is how it appears in file 4.Again, thank you very much for trying to help me. I do sincerely appreciate all your efforts.
Link to comment
Share on other sites

The htmlspecialchars() function returns the content in a way that is suitable for display, instead of being suitable for HTML (i.e. it will prevent the browser from interpreting the <br /> tag as a new line and will instead show it as text).Simply removing the htmlspecialchars() function should do the trick, i.e.

print implode("<br />", file($fn) );

Link to comment
Share on other sites

There are descriptions and examples of every single PHP function in the manual, e.g.:http://www.php.net/manual/en/function.nl2br.phpIt describes the function syntax:string nl2br ( string $string [, bool $is_xhtml = true ] )That means the function returns a string value, and it accepts 2 parameters, one is a string parameter and it's required, and the other is an optional boolean parameter.It also lists a concise description about what the function does:Returns string with '<br />' or '<br>' inserted before all newlines.That's all it does, there's nothing else to it.Then it describes the parameters to the function:stringThe input string.is_xhtmlWhether to use XHTML compatible line breaks or not.It also says what it returns:Return ValuesReturns the altered string.After that it gives 2 examples, then a changelog listing changes to the function between PHP versions, then a list of related functions, and below all of that are user comments talking about how to use the function, other functions people have written to do similar things, cautions to watch out for, etc. I really wasn't trying to be that vague, I had assumed that pointing you towards nl2br would lead you to the manual page which should answer any question you had about how nl2br works or how to use it.If you're just trying to print the contents of a text file on a page, instead of using file and implode it's easier to use file_get_contents:<?php$fn = "test.txt";print nl2br(htmlspecialchars(file_get_contents($fn)));?>That line gets the file contents, the runs htmlspecialchars on the output from file_get_contents, then runs nl2br on the output from htmlspecialchars.

Link to comment
Share on other sites

Thank you so much everyone.I of course tried both examples and found both work well. So I have accomplished my original goal.More importantly, your explanations have gone a long way in helping me understand the why and how these scripts actually work. For that knowledge I am truly grateful.Best regards,Jeff

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...