Double space from textarea

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
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Double space from textarea

Post by sleepydad »

I'm sure there's a simple solution to this. I have a PHP page with the following (edited for brevity) ...

Code: Select all

 
$bodyFromEmail=$_POST['message'];
$bodyFromEmail=stripslashes($bodyFromEmail);
$bodyFromEmail=str_replace("\r", "\n", $bodyFromEmail);
 
mail ($to, $subject, $bodyFromEmail, $headers);
 
The $bodyFromEmail is capture in PHP using

Code: Select all

 
echo "<textarea name='message' cols='50' rows='10' wrap='virtual'></textarea>";
 
When I send a test to myself, it comes across as double spaced in my email client. What can I do to fix this?

Thanks in advance -
sleepydad
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Double space from textarea

Post by requinix »

Windows uses \r\n for newlines. If you replace each \r with a \n then you get \n\n.

Code: Select all

$bodyFromEmail=preg_replace('/\r\n?/', "\n", $bodyFromEmail);
That will replace Windows's \r\n and Mac's \r with a Unix \n.
sleepydad
Forum Commoner
Posts: 75
Joined: Thu Feb 21, 2008 2:16 pm

Re: Double space from textarea

Post by sleepydad »

That did it.

Muchas gracias.
Post Reply