PHP如何将HTML字符串保存到数据库中 [英] PHP how to save HTML string into database
问题描述
为了响应API调用,我收到了一个完整的HTML脚本。完全意味着它包括HTML CSS和Javascript。现在我把那个HTML作为PHP变量中的字符串。
In response to an API call, I'm getting a full HTML script. Full mean 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?
- 作为VARCHAR或TEXT类型的字符串?
- 作为字符串,使用Base64编码VARCHAR或TEXT类型?
- 作为具有BLOB类型的二进制文件?
或者其他任何你想要的喜欢建议(可能是Serialize或Pack)?
Or any other you would like to suggest(May be Serialize or Pack)?
推荐答案
我使用base64编码数据以BLOB数据类型存储在我的数据库中。样板代码如下。
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>';
在base64中编码数据
To encode data in base64
$encodedContent = base64_encode($content); // This will Encodes
并使用BLOB将数据保存在数据库中。现在在检索数据后,只需将其解码如下。
And save the data in database with BLOB. Now after retrieve data, just decode it as follow.
$ContentDecoded = base64_decode($content); // decode the base64
现在 $ contentDecoded 的值是普通的HTML。
Now the value of $contentDecoded is the plain HTML.
这篇关于PHP如何将HTML字符串保存到数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!