duan35557593 2015-07-26 05:40
浏览 87

PHP - textarea的换行符不起作用

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

  • 写回答

1条回答 默认 最新

  • dtu72460 2015-07-26 06:04
    关注

    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 //
      "
    " => '<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;">'
    );
    

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

    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法