首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP -文本区域中的换行符不起作用

PHP -文本区域中的换行符不起作用
EN

Stack Overflow用户
提问于 2015-07-26 13:40:13
回答 1查看 245关注 0票数 0

我今天已经写了一个脚本,用于格式化表单中HTML电子邮件中的文本。在我输入邮件正文的地方,我使用了一个文本区域。我已经尝试了一整天,试图弄清楚当我在文本区域中按enter换新行时,新行也会进入电子邮件中的什么位置。

到目前为止,我已经尝试过str_replace()preg_replace()nl2br()

我甚至将这三个代码合并到一个代码中,但这根本不起作用。

谁能告诉我为什么当我的文本有换行符时,我的电子邮件中没有任何<br>标签?

PHP代码:

代码语言:javascript
复制
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;
}

请注意,如果一切可能的话,我希望尽可能接近我的脚本。正如你所看到的,我正在用它来替代很多东西,这是相当容易使用的。

这只是一个测试脚本,所以我要替换的每一段数据都还没有输入。

谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-07-26 14:04:32

将array中的单引号(')替换为双引号(")。

代码语言:javascript
复制
$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;">'
);

我之前也遇到过同样的问题。使用单引号的换行符(\n)不起作用,而是使用双引号。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31634028

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档