php - 在数据库中存储 ckeditor 值(html 标记)的正确方法? (换行问题)

标签 php mysql ckeditor newline

我是第一次使用 ckeditor,我正在使用 php 将 ckeditor 的值存储到 mysql 数据库中:

$content = mysql_real_escape_string($_POST['content']);

当我在插入数据库之前使用 mysql_real_escape_string 时,它会在来自 ckeditor 的 html 中添加\r\n 。这是存储在数据库中的内容:

<p>Here is a line</p>
\r\n
<pre class=\"code-python\">name = &quot;Bob&quot;
\r\n
last = &quot;Smith&quot;
\r\n
full = name + &quot; &quot; + last</pre>
\r\n
<p>And another line</p>
\r\n

这是我将其回显给浏览器时看到的内容:

Here is a line

rn

name = "Bob"rnlast = "Smith"rnfull = name + " " + last

rn

And another line rn

请注意,我确实需要在 pre 标签中保留换行符,所以我不能简单地删除所有换行符。

这是我想看到的:

Here is a line

name = "Bob"

last = "Smith"

full = name + " " + last

And another line

最佳答案

将此行添加到您的代码中:$content = str_ireplace('\r\n', '', $content);

所以你的代码现在应该是:

$content = mysql_real_escape_string($_POST['content']);
$content = str_ireplace('\r\n', '', $content);

我试过了,它有效。

关于php - 在数据库中存储 ckeditor 值(html 标记)的正确方法? (换行问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9270370/

相关文章:

mysql - 将数据透视表连接到另一个表(HABTM 连接 HABTM 和模型关联)

javascript - 在 Angular 2 中鼠标悬停之前,组件变量更改不会在 View 中更新

CKEditor 格式标签和自定义 <small> 标签

php - 从数据库中获取多个字段到数组

mysql - SQL 返回 NULL 而不是 0 行

PHP & mySQL : When exactly to use htmlentities?

mysql - 如何在 OS X 上从一个本地端口转发到另一个本地端口?

javascript - CKEditor 渲染 Angular 自定义指令

php - 为 Amazon Marketplace API 计算签名的问题

php - 如何从平面数组创建多维树数组?