Store HTML code in the database? [closed]

0

I am developing a script that retrieves HTML tags from a form, and then store it in the database. However I would like to store the code in the base as it was entered in the form, with the tags, style, classes, etc ...

OBJECTIVE :

  • For example, if I store the code

    <div style='color:red;'>Meu texto</div>

When reading this information (with PHP for example: echo $informacoes_da_base ) I would like to get "My text" in red.

PROBLEM :

  • Tags are being interpreted as soon as they arrive at the base. Ex: &lqt;code &lql;

  • When reading the information in the database eg echo $informacoes_da_base I get unwanted characters on the screen.

I did some research on mysql_escape_string and html_entity_decode but I can not get the results you want.

echo html_entity_decode($infos_da_base);

echo mysql_escape_string($infos_da_base);

CODE :

if(isset($_POST['enregistrer'])){

  Configuration::updateValue('DESCRIPTION_TIME_DELIVERY', $_POST['description']);

}
<form>
   <textarea style="width: 200px;">
       <code style='color: red;'>codigo html aqui</code>
   </textarea>
</form>
    
asked by anonymous 31.01.2017 / 15:45

1 answer

1

Use htmlentities() to encode these characters and then the tags will look like this: &lt;/img&gt; this encoding is: <img/> , done that is just save in the BD.    At the time of returning this data if you are to play them directly in the DOM so do not need to pass function to decode because the browser already does this, now if you are to put it inside some input or textarea pass before in function html_entity_decode() to return the original characters.

Summary:

<?php
$a = htmlentities('<img/>'); //Retorno "&lt;/img&gt;"
$b = html_entity_decode($a); //Retorno "<img/>"
    
31.01.2017 / 16:02