What is the best way to save small PHP code in the MySQL database so that it could be viewed, along with text saved in HTML in the database?
What is the best way to save small PHP code in the MySQL database so that it could be viewed, along with text saved in HTML in the database?
If you want to convert these codes to use as an example in your application ... Here is the tip.
Use the htmlentities
function to encode PHP code. This function will convert all applicable characters into HTML entities, for example:
This code
<?php
$variable = ["A", "B", "C"];
var_export($variable);
?>
Will be transformed into:
<?php
$variable = ["A", "B", "C"];
var_export($variable);
?>
Once you've done this, you'll be able to use it in your HTML as usual.
Meu código:
<pre>
<code>
<?php
$variable = ["A", "B", "C"];
var_export($variable);
?>
</code>
</pre>
To use this function, simply do the following:
<?php
$str = "A 'quote' is <b>bold</b>";
// Outputs: A 'quote' is <b>bold</b>
echo htmlentities($str);
?>