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!
Answer found in the documentation comments for nl2br:
Source: http://php.net/manual/en/function.nl2br.php#91828