使用 php 在 mysql 数据库中存储数据时如何保留文本格式?

How to preserve formatting of text while storing data in mysql database using php?

我正在使用 HTML5 文本编辑器,人们可以使用它

  1. 制作文字bold/italics。
  2. 插入link/image。
  3. 更改文本颜色,添加 bullet/numbers 等

如何存储和检索此数据 in/from mysql 数据库以便保留格式?目前,文本正在存储,但以纯文本形式存储。

我应该采用什么方法来解决这个问题?

使用htmlentities()函数并在MySQL中使用TEXT作为属性的字段类型

让元素有id editor 然后在js 使用

$('#editor').html();

示例:

<div id="editor">
  <!--Here goes your content from HTML5 Editor-->
</div>

所以用AJAX保存:

function StoreText() {
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            if (xmlhttp.responseText == "true") {
                //success message
            } else {
                //error message
            }
        }
    }
    xmlhttp.open("POST", "Update.php", true);
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.send("value=" + $('#editor').html());
}