PHP how to save HTML string into database

2020-04-07 06:13发布

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.

$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?

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

6条回答
【Aperson】
2楼-- · 2020-04-07 06:35

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

查看更多
Lonely孤独者°
3楼-- · 2020-04-07 06:36

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 structure for 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.

查看更多
家丑人穷心不美
4楼-- · 2020-04-07 06:45

Store HTML into a variable using addslashes() function.

$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 = "UPDATE `Pages` SET `content`= '".$html."'";

and you have to add stripslashes when retrieve from DB

查看更多
再贱就再见
5楼-- · 2020-04-07 06:47

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

$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

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

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

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

Now the value of $contentDecoded is the plain HTML.

查看更多
Deceive 欺骗
6楼-- · 2020-04-07 06:48

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

    $editorContent = $_POST['textarea'];

    $a = $editorContent;

    $insert = $db->query("INSERT INTO psd ( psdata) VALUES ( '$a')" );
查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-04-07 06:52

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

Data type to store HTML in Database would be TEXT.

Use mysql_real_escape_string() to store html text in database

$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);
查看更多
登录 后发表回答