php PHP如何将HTML字符串保存到数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37338770/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:10:42  来源:igfitidea点击:

PHP how to save HTML string into database

phpmysqldatabasestringhtml

提问by saiid

In response to an API call, I'm getting a full HTML script. Full means it includes HTML CSS and Javascript. Now I have that HTML as string in PHP variable.

为了响应 API 调用,我得到了一个完整的 HTML 脚本。完整意味着它包括 HTML CSS 和 Javascript。现在我在 PHP 变量中将该 HTML 作为字符串。

$content = '<html>
<head>
  <script>--Some javascript and libraries included--</script>
  <title></title>
</head>
<body>
   <style>--Some Styling--</style>
</body>
</html>';

Now, what is the best way to save this variable in Database and How?

现在,在数据库中保存这个变量的最佳方法是什么?

  • As a string with VARCHAR or TEXT type?
  • As a string with Base64 Encoded with VARCHAR or TEXT type?
  • As a Binary with BLOB type?
  • 作为具有 VARCHAR 或 TEXT 类型的字符串?
  • 作为带有 VARCHAR 或 TEXT 类型的 Base64 编码的字符串?
  • 作为具有 BLOB 类型的二进制文件?

Or any other you would like to suggest(May be Serialize or Pack)?

或者你想推荐的其他任何东西(可能是序列化或包装)?

回答by saiid

I use base64 encoded data to store in my Database with the BLOB datatype. The boilerplate code is as follow.

我使用 base64 编码的数据以 BLOB 数据类型存储在我的数据库中。样板代码如下。

$content = '<html>
<head>
  <script>--Some javascript and libraries included--</script>
  <title></title>
</head>
<body>
   <style>--Some Styling--</style>
</body>
</html>';

To encode data in base64

以 base64 编码数据

$encodedContent = base64_encode($content); // This will Encodes

And save the data in database with BLOB. Now after retrieve data, just decode it as follow.

并使用 BLOB 将数据保存在数据库中。现在检索数据后,只需按如下方式对其进行解码。

$ContentDecoded = base64_decode($content);  // decode the base64

Now the value of $contentDecodedis the plain HTML.

现在$contentDecoded的值是纯 HTML。

回答by e4c5

If you base64 encode it you increase the storage size by rougly 30% and you need to decode it each time you display it. Look at the table structurefor Wordpress, the most widely used software that stores html on a mysql database using php. What do they use? LONGTEXT. In your case TEXT is probably better because you probably have a good idea about the size of the page.

如果对它进行 base64 编码,则存储大小大约会增加 30%,并且每次显示时都需要对其进行解码。看一下Wordpress的表结构,它是使用最广泛的软件,它使用 php 将 html 存储在 mysql 数据库中。他们用什么?长文。在您的情况下, TEXT 可能更好,因为您可能对页面的大小有一个很好的了解。

回答by Gorakh Yadav

Store HTML into a variable using addslashes()function.

使用addslashes()函数将HTML 存储到变量中。

$html = addslashes('<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p>&nbsp;</p>
</div>
</div>
</div>');

After this, form an SQL query.

在此之后,形成一个 SQL 查询。

$sql = "UPDATE `Pages` SET `content`= '".$html."'";

and you have to add stripslasheswhen retrieve from DB

并且您必须stripslashes在从数据库检索时添加

回答by Jignesh Patel

you can use base64_encode and store that string into db with text/blob type of field

您可以使用 base64_encode 并将该字符串存储到带有 text/blob 类型字段的数据库中

回答by Manjeet Barnala

I would recommend you to use TEXT. Blobs are typically used to store images, audio or other multimedia objects. read more about bolobs

我建议你使用TEXT. Blob 通常用于存储图像、音频或其他多媒体对象。阅读更多关于bolobs

Data type to store HTML in Database would be TEXT.

在数据库中存储 HTML 的数据类型是TEXT.

Use mysql_real_escape_string()to store html text in database

使用mysql_real_escape_string()在数据库中存储 html 文本

$content = '<html>
<head>
  <script>--Some javascript and libraries included--</script>
  <title></title>
</head>
<body>
   <style>--Some Styling--</style>
</body>
</html>';

$html = mysql_real_escape_string($content);

回答by Knowledge Seeker

See this: I did nothing... i use it like this ..working just fine and also saving in mysql and also retreving back properly.

看到这个:我什么都没做……我像这样使用它……工作得很好,也保存在 mysql 中,也能正常恢复。

    $editorContent = $_POST['textarea'];

    $a = $editorContent;

    $insert = $db->query("INSERT INTO psd ( psdata) VALUES ( '$a')" );