为了响应 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>';
现在,在数据库中保存此变量的最佳方式是什么?如何操作?
- 作为 VARCHAR 或 TEXT 类型的字符串?
- 作为 Base64 编码的字符串,使用 VARCHAR 或 TEXT 类型?
- 作为 BLOB 类型的二进制文件?
或者你想建议的任何其他(可能是序列化或打包)?
最佳答案
我使用 base64 编码数据以 BLOB 数据类型存储在我的数据库中。样板代码如下。
$content = '<html>
<head>
<script>--Some javascript and libraries included--</script>
<title></title>
</head>
<body>
<style>--Some Styling--</style>
</body>
</html>';
将数据编码为base64
$encodedContent = base64_encode($content); // This will Encode
并用BLOB 将数据保存在数据库中。现在检索数据后,只需按如下方式对其进行解码。
$ContentDecoded = base64_decode($content); // decode the base64
现在 $contentDecoded 的值是纯 HTML。
关于PHP如何将HTML字符串保存到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338770/