Entity
Entity

Reputation: 8202

HTML textarea newline character

I have a text area in HTML where the user can enter text, but when the form is submitted, and its passed to a php script which echo's it, there aren't any newlines. Knowing that HTML does that, I tried doing a preg_replace() before echoing it...

    echo preg_replace("/\n/", "<br />", $_GET["text"]);

but still everything is on one line.

So my best guess is that HTML textareas use a different newline character... can anybody shed some light on the subject?

EDIT

Ok, so I've figured out the problem: The Javascript is stripping the newlines. view code here

EDIT 2

Ok, so thanks to Jason for solving this problem. I needed to do:

    escape(document.getElementById('text'));

Instead of just:

    document.getElementById('text');

and the newlines are preserved, problem solved!

Upvotes: 4

Views: 8228

Answers (2)

Neon Physics
Neon Physics

Reputation: 3477

echo nl2br($_GET['text'])

Though, your preg_replace worked for me!

Upvotes: 3

Jesse
Jesse

Reputation: 10466

usually when testing for newlines in any string, I use /[\n\r]/, just to cover my bases. My guess is that this would match the new lines.

Upvotes: 0

Related Questions