![](/img/trans.png)
[英]How to save a html with php function and html tags into a php variable as string?
[英]PHP how to save HTML string into database
为了响应 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>';
现在,在数据库中保存这个变量的最佳方法是什么?
或者你想推荐的其他任何东西(可能是序列化或包装)?
我使用 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。
如果对它进行 base64 编码,则存储大小大约会增加 30%,并且每次显示时都需要对其进行解码。 看一下 Wordpress 的表结构,它是使用最广泛的软件,它使用 php 将 html 存储在 mysql 数据库中。 他们用什么? 长文。 在您的情况下, TEXT 可能更好,因为您可能对页面的大小有一个很好的了解。
使用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> </p>
</div>
</div>
</div>');
在此之后,形成一个 SQL 查询。
$sql = "UPDATE `Pages` SET `content`= '".$html."'";
从数据库中检索时,您必须添加stripslashes
您可以使用 base64_encode 并将该字符串存储到带有 text/blob 类型字段的数据库中
我建议您使用TEXT
。 Blob 通常用于存储图像、音频或其他多媒体对象。 阅读更多关于bolobs
在数据库中存储 HTML 的数据类型将是TEXT
。
使用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);
看到这个:我什么都没做...
我像这样使用它..工作得很好,也保存在 mysql 中,也能正确检索。
$editorContent = $_POST['textarea'];
$a = $editorContent;
$insert = $db->query("INSERT INTO psd ( psdata) VALUES ( '$a')" );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.