[SOLVED]Preserving New Line Breaks in an Email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
waboo386
Forum Newbie
Posts: 6
Joined: Sun Aug 03, 2003 9:15 pm

[SOLVED]Preserving New Line Breaks in an Email

Post 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
Last edited by waboo386 on Thu Jun 24, 2004 3:57 pm, edited 1 time in total.
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

I haven't tested this, but how about nl2br("your string"). Check the manual on this...
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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.
waboo386
Forum Newbie
Posts: 6
Joined: Sun Aug 03, 2003 9:15 pm

Post by waboo386 »

Okay thanks :)
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i would use str_replace over RegEx in this case.
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

Yes you are right, since we do not need the replacing rules in this manner using str_replace is better ;)
Post Reply