我今天已经写了一个脚本,用于格式化表单中HTML电子邮件中的文本。在我输入邮件正文的地方,我使用了一个文本区域。我已经尝试了一整天,试图弄清楚当我在文本区域中按enter换新行时,新行也会进入电子邮件中的什么位置。
到目前为止,我已经尝试过str_replace()、preg_replace()和nl2br()。
我甚至将这三个代码合并到一个代码中,但这根本不起作用。
谁能告诉我为什么当我的文本有换行符时,我的电子邮件中没有任何<br>标签?
PHP代码:
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;
}请注意,如果一切可能的话,我希望尽可能接近我的脚本。正如你所看到的,我正在用它来替代很多东西,这是相当容易使用的。
这只是一个测试脚本,所以我要替换的每一段数据都还没有输入。
谢谢
发布于 2015-07-26 14:04:32
将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;">'
);我之前也遇到过同样的问题。使用单引号的换行符(\n)不起作用,而是使用双引号。
https://stackoverflow.com/questions/31634028
复制相似问题