Noah Goodrich
Noah Goodrich

Reputation: 227

Replacing \r\n with <br />

Basically i'm trying to set something where a user enters some text into a textarea (and they press their enter key to add new lines) and then it takes the value from that text area and mails it out in the message of a new email to a specified contact.

All the mailing functionality works, but I need to be able to automatically add a new line when the user presses enter in the textarea.

Using this to get the info from the previous page/form and replace the \r\n:

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

Then just adding $NEWS into the code to mail it out.

Doesn't seem to work though, just puts it on 1 line and doesn't replace the \r\n!

Any help is most appreciated :)

Upvotes: 4

Views: 2346

Answers (4)

genesis
genesis

Reputation: 50976

nl2br really works.

http://sandbox.phpcode.eu/g/0e270

Upvotes: 2

Alexander Sulfrian
Alexander Sulfrian

Reputation: 3563

Try to use nl2br. It will respect all different formats like \n, \r or \r\n (http://de.php.net/manual/en/function.nl2br.php)

Upvotes: 1

Holger Just
Holger Just

Reputation: 55758

There's the nl2br function built-in to PHP which does exactly what you want to achieve.

Upvotes: 1

Quentin
Quentin

Reputation: 943564

Just use the nl2br built in

Upvotes: 4

Related Questions