简体   繁体   中英

PHP - Line breaks from textarea not working

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 //
  '\n' => '<br>',
  '\r' => '<br>',
  '\r\n' => '<br>',
  '\n\r' => '<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( "/\r|\n/", "", $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

Replace the single quote (') with double quote (") in array.

$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 //
  "\n" => '<br>',
  "\r" => '<br>',
  "\r\n" => '<br>',
  "\n\r" => '<br>',
  PHP_EOL => '<br>',  
  // Filter negative words //
  "badword1" => '********',
  "badword2" => '********',
  // HTML to convert to HTML with inline styles //
  "<h1>" => '<h1 style="color: #fff;">'
);

I had the same problem earlier. new line(\\n) with single quote does not work but double quote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM