Page 1 of 1

[SOLVED]Preserving New Line Breaks in an Email

Posted: Thu Jun 24, 2004 12:59 pm
by waboo386
Hi

So say I have a form that is meant to collect data from users. At the moment, other than stripslashes I am not modifying the content...it simply comes to my inbox.

Now say the user says something like this:

"Hello

My name is Rob

Look at me"

What *I'll* receive in my inbox is

"Hello My name is Rob Look at me".

So my question is this: how would I modify the text so that the new line breaks are somehow preserved in the email message?

Thanks a lot

Rob

Posted: Thu Jun 24, 2004 1:12 pm
by wwwapu
I haven't tested this, but how about nl2br("your string"). Check the manual on this...

Posted: Thu Jun 24, 2004 1:17 pm
by dethron
Yes nl2br() is works well in your problem.
Moreover if you want to convert <br> back to \n this may be helpfull.

Code: Select all

<?php
function br2nl( $data ) {
   return preg_replace( '!<br.*>!iU', "\n", $data );
}
?>
And you may find more on the manual.

Posted: Thu Jun 24, 2004 1:19 pm
by waboo386
Okay thanks :)

Posted: Thu Jun 24, 2004 1:19 pm
by tim
i would use str_replace over RegEx in this case.

Posted: Thu Jun 24, 2004 1:21 pm
by dethron
Yes you are right, since we do not need the replacing rules in this manner using str_replace is better ;)