Question:
Make PHP recognize line returns?
Shawna C
2009-09-17 16:42:37 UTC
I'm trying to build a PHP/CMS thingy and it's all going great. But I just can't figure out how to make the simple line returns the user types into the text field show up once the text has been saved and included in the actual page. Is this something that I do on the "PHP Include" end or on the "save" end in the doc with the form in it???

Here's the code if anyone feels very generous and could add the right bit in the right place to make this happen! Thanks.

$saving = $_REQUEST['saving'];
if ($saving == 1) {
$data = $_POST['data'];
$file = "text_files/a1txt.txt";
$fp = fopen($file, "w") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");

fclose($fp);
echo "Saved to $file successfully!";
}
?>







Three answers:
2009-09-17 17:16:35 UTC
I don't quite understand what you are asking for. I could proabably answer this, but your grammar makes it difficult to understand what you are wanting to be added to your script.

Feel free to email me and revise/clear up your question so I can help you out.



The only problem I see however is this:


$file = "text_files/a1txt.txt";

if (!empty($file)) {

$file = file_get_contents("$file");

echo $file;

}

?>

If the content of .txt had any html code saying the script would leave the box and actually output onto the page.

To fix this, simply add:


$file = "text_files/a1txt.txt";

if (!empty($file)) {

$file = file_get_contents("$file");

$file = URLencode("$file");

echo $file;

}

?>



But unless that's what you were looking for, i suggest you contact me either Instant messenger or email. Sorry.
2016-12-03 07:33:29 UTC
Line breaks in HTML (no count if hand-written or generated through something like own living house page) get condensed with different whitespace right into a unmarried area for each series of consecutive whitespace characters. in case you desire to insert a newline into an HTML checklist, use the br tag. Or, extra beneficial yet, enclose each paragraph in a

element. in case you do no longer desire to contain the HTML tags interior the .txt report you're interpreting from, merely replace all of them with "br" when you have examine them from the report, yet in the previous you output the text cloth to the information superhighway internet site.

Cristiano
2009-09-17 17:31:15 UTC
You will need to encode the "\nl" (or line breaks) from the text to
tags. There is a simple function from PHP that you can use: nl2br($string);



Check PHP.net for more info on the function.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...