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?
Or any other you would like to suggest(May be Serialize or Pack)?
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 Encode
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With