textarea new line character problem

First, everuthing was working fine and now it's not.

If I type in the textarea of my form


using the enter key to go to a new line, it now insert in the db


I used

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

to replace the new line character, I also try nl2br() function to no avail and like I said it was working fine before, any advice?
LVL 28
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
you should save the data as entered, with the  \n (and \r) ...
only after retrieving and putting back into a textarea you have to use the nl2br() of that value to get the "proper" value as html with line breaks

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gamebitsAuthor Commented:
Thanks for the quick answer angelIII, I must have another problem then because if I echo the post sent by the form it display the \r\n, I understand it would not be on a different line if I don't convert the new line character to <br> but I don't thing I should see \r\n displayed in the browser, shoud I?
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>but I don't thing I should see \r\n displayed in the browser, shoud I?
I assume, you means page -> view source.
yes, you can see it there, no reason you should not see it there...
and it is "good" to have it in the post data, otherwise you could not save / replace it when needed...
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

gamebitsAuthor Commented:
No, I mean in the browser window not the source.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you post the link to the web page (if it is public)?
can you post the relevant part of the code that handles $text more ie fills it into the textarea/db?
gamebitsAuthor Commented:
Quick update:

The site is not public yet, I include a file to check user access level and if I remove that file the script work perfectly, that file as 3 more files included so I'll have to check what is causing this behavior, thanks for your help so far.
gamebitsAuthor Commented:
Haven't found what is causing this strange behavior, I fixed the problem by changing the authenticate script, I'm now using session instead of cookies which I'm sure had nothing to do with my original problem.

But now it work as intended anyway.

Thanks for the clarification on how to use nl2br function.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.