我是第一次使用 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 = "Bob"
\r\n
last = "Smith"
\r\n
full = name + " " + 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/