I have written a script today for formatting text inside of my HTML emails from my form. Where I enter the message body of my email I use a textarea. I have been trying all day to figure out how to make it to where when I press enter
for a new line in the textarea, that new line also goes into the email as well.
So far I have tried str_replace()
, preg_replace()
, and nl2br()
.
I've even gone as far as combining all three into one code, but that didn't even work.
Could someone tell me why I am not getting any <br>
tags in my emails when my textareas have line breaks?
PHP Code:
function replaceText($msg) {
$replaceableText = array(
// Emoticons to replace //
'xD' => '<img src="emoticons/devil.png" height="18" width="18">',
'>:)' => '<img src="emoticons/devil.png" height="18" width="18">',
'x(' => '<img src="emoticons/angry.png" height="18" width="18">',
':((' => '<img src="emoticons/cry.png" height="18" width="18">',
':*' => '<img src="emoticons/kiss.png" height="18" width="18">',
':))' => '<img src="emoticons/laugh.png" height="18" width="18">',
':D' => '<img src="emoticons/laugh.png" height="18" width="18">',
':-D' => '<img src="emoticons/laugh.png" height="18" width="18">',
':x' => '<img src="emoticons/love.png" height="18" width="18">',
'(:|' => '<img src="emoticons/sleepy.png" height="18" width="18">',
':)' => '<img src="emoticons/smile.png" height="18" width="18">',
':-)' => '<img src="emoticons/smile.png" height="18" width="18">',
':(' => '<img src="emoticons/sad.png" height="18" width="18">',
':-(' => '<img src="emoticons/sad.png" height="18" width="18">',
';)' => '<img src="emoticons/wink.png" height="18" width="18">',
';-)' => '<img src="emoticons/wink.png" height="18" width="18">',
// Line breaks to replace //
'
' => '<br>',
'' => '<br>',
'
' => '<br>',
'
' => '<br>',
PHP_EOL => '<br>',
// Filter negative words //
'badword1' => '********',
'badword2' => '********',
// HTML to convert to HTML with inline styles //
'<h1>' => '<h1 style="color: #fff;">'
);
foreach($replaceableText as $replace => $replacedWith) {
$msg = str_replace($replace, $replacedWith, $msg);
}
$msg = preg_replace( "/|
/", "", $msg );
$msg = nl2br($msg);
return $msg;
}
Please note that if at all possible I would like to remain as close to my script as possible. As you can see I am using it to replace a lot of things and this is fairly easy to use.
This is just a test script so every piece of data I am going to replace is not yet entered.
Thanks