简体   繁体   中英

Line breaks / returns in a text area retrieved with php

EDIT: My problem simplified I suppose has boiled down to not being able to completely remove the new line character.

I tried: $new_content = str_replace("\\n", '<br/>', $_POST["new_content"]); And I can't see a reason why this wouldn't work.. but when I inspect the text file I still have a mysterious new line like so:

event:|:Test:|:Line 1
<br/>Line 2:|:September 29th, 2012

Original question:

I have a form with a text field and a VERY simple 'database', that's just a .txt file, that i am storing the submitted form data in. It all works well aside from one major issue i've just noticed. If a user presses the enter/return key to go to a new line in the text box, whatever the resultant string is it messes up my text file where a new line signifies another row in the database.

For examples here's what the text file typically should look like:

type:|:heading:|:content:|:date
event:|:Big Event 2012:|:More info and booking details.:|:September 29th, 2012
event:|:Website relaunch:|:Keep your eyes pealed.:|:September 25th, 2012

Now if i were to fill out the form pressing the return key in the text box to start a new paragraph my text file looks like this:

type:|:heading:|:content:|:date
event:|:Test:|:paragraph 1
paragraph 2:|:September 29th, 2012
event:|:Website relaunch:|:Keep your eyes pealed.:|:September 25th, 2012

As you can see the string has resulted in a new row being used in the database whereas i would expect / desire something like..

event:|:Test:|:paragraph 1 \n paragraph 2 :|:September 29th, 2012 //or
event:|:Test:|:paragraph 1 <br/> paragraph 2 :|:September 29th, 2012

I did try looking for similar questions online but all the solutions pointed towards using something like nl2br($_POST["new_content"]); but that seems to produce a text file formatted like this:

type:|:heading:|:content:|:date
event:|:Test:|:paragraph 1<br />

paragraph 2:|:September 29th, 2012
event:|:Website relaunch:|:Keep your eyes pealed.:|:September 25th, 2012

Where the original problem still persists. Can someone explain why this happens or do you have any suggestions for how i should be formatting the string retrieved by $_POST["new_content"] before saving it in the text file? Would appreciate any help!

Try

$_POST["new_content"] = str_replace("\n", '<br/>', $_POST["new_content"]);

Edit:

You can also try:

$_POST["new_content"] = ( preg_replace( '/\s+/', '<br/>', $_POST["new_content"] ) ); 

You have a fundamental issue of special characters. A line break is a special character in your file format. You have two choices: either don't allow line breaks in the saved content, or escape/encode the line break in some way, for example by replacing it by \\n before writing into the file. That also means that \\n is a special character though, so you need to escape an actual \\n that may occur in the text somehow.

See http://kunststube.net/escapism for more general information on special characters and escaping. You either need to come up with your own rules for your file format, or adopt some established format like XML.

Try this on your processing file for saving new content into txt file: It's like @TheShiftExchange Answer just add new line break on variable content before saving it into txt file.

$file= 'log.txt';
$content= preg_replace('/\s+/', '', $_POST['new_content']);
$content.= "\n";
$fh= fopen($file, 'a+');
fwrite($fh, $content);
fclose($fh);
$all_contents= file_get_contents($file);
echo nl2br($all_contents);

Answer found in the documentation comments for nl2br:

strtr($text, array("\r\n" => '<br />', "\r" => '<br />', "\n" => '<br />'));

Source: http://php.net/manual/en/function.nl2br.php#91828

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM