现在它只是添加
\r\n\r\n
我正在尝试找出如何转换
\r \n to <p> tags
HTML 通过 XHR 发布到数据库,并在 firebug 中发布,如下所示:
html:
<html><head>\r\n<meta http-equiv=Content-Type content=text/html; charset=utf-8>\r\n</head>\r\n<body>\r\n<pre style=word-wrap:break-word; font-size:10.0pt; font-family:Tahoma; color:black>YADA YADA YADAYADA YADA YADAYADA YADA YADAYADA YADA YADA!\n\n YADA YADA YADAYADA YADA YADAYADA YADA YADAYADA YADA YADA.\n\nYADA YADA YADA\n\n< via mobile device >\n\nYADA YADA YADA <xxxx@xxxxx.com> wrote:\n\n</pre>\r\n<div>\r\n<table cellpadding=0 cellspacing=0 border=0 width=700 style=padding:0;>\r\n<tbody>\r\n<tr>\r\n<td colspan=2 style=color:#000000;font-size:11px;font-family:'lucida grande', tahoma, verdana, arial, sans-serif valign=top>\r\n<div style=width:100%;font-size:12px;font-family:Helvetica;line-height:18px;>..... and it keeps going.
但是当我使用 simple_format 时:
body = simple_format(@html)
我明白了:
<p>\r\n\r\n\r\n\r\n<pre>YADA YADA YADAYAD........
为什么 simple_format 不去掉\r 和\n 并使用段落?
感谢您的宝贵时间。
最佳答案
您最好先进行字符串替换:
@html.gsub!(/\r\n?/, "\n"); body = simple_format(@html)
这是因为 simple_format 仅适用于\n 和\n\n,不适用于\r\n:
Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in
tags. One newline (\n) is considered as a linebreak and a
tag is appended. This method does not remove the newlines from the text.
关于ruby-on-rails - 如何在 Ruby on Rails 中将\r\n 转换为简单格式的 <p>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5201433/