Home > other >  PHP : format an html <div> to display it correctly
PHP : format an html <div> to display it correctly

Time:12-08

Hello I want to get this but when I store my div in my database and when I try to display it I can't display it correctly can you help me?

Desired outcome:

<div data-tpl="header1" data-title="Header 1" class="drop-element"><label>LABEL 111</label></div>
<div data-tpl="header2" data-title="Header 2" class="drop-element"><input class="form-control" value="" id="changeInput" placeholder="INPUT 222"></div>

In my database :

<div data-tpl=\"header1\" data-title=\"Header 1\" class=\"drop-element\"><label>LABEL 111</label></div>
<div data-tpl=\"header2\" data-title=\"Header 2\" class=\"drop-element\"><input class=\"form-control\" value=\"\" id=\"changeInput\" placeholder=\"INPUT 222\"></div>

Display on the web page using this function on my page.php but the display is not correct :

preg_replace('/\\\\/', '', $formChaine);

unwanted result I have on the web page:

<div data-tpl="\&quot;header1\&quot;" data-title="\&quot;Header" 1\"="" class="\&quot;drop-element\&quot;"><label>LABEL 111</label></div>
<div data-tpl="\&quot;header2\&quot;" data-title="\&quot;Header" 2\"="" class="\&quot;drop-element\&quot;"><input class="\&quot;form-control\&quot;" value="\&quot;\&quot;" id="\&quot;changeInput\&quot;" placeholder="\&quot;INPUT" 222\"=""></div>

CodePudding user response:

Everything is being escaped probably as a security measure to protect your database from sequel injection. You need to unescape it to display it. You could do it on the php side with stripcslashes when you read it from the database: https://www.php.net/manual/en/function.stripcslashes.php.

  • Related