2

I have a string(variable) having value of database cell. Actually that value is html code. I have to print that code into html page. I found that how to convert HTML string into DOM string. But I don't know that How to convert string into HTML string. If anyone know answer then please explain or suggest me link from where I can understand from beginning. Thank You. Here is my code.

<?php
$dbstr = mysql_escape_string("&lt;p&gt;Just install and use this app.&amp;nbsp;&lt;strong&gt;MENU -&amp;gt; SETTINGA&lt;/strong&gt;&lt;/p&gt;
");
?>

<!DOCTYPE html>
<html>
<body>
    <script type="text/javascript">
        function fun(str) {
            var div = document.getElementById('htmldiv');
            div.innerHTML = str;
            var elements = div.childNodes;
        }
    </script>
    <div id="htmldiv">
        <script type="text/javascript">
            fun('<?php echo $dbstr; ?>');
        </script>
    </div>
</body>

I get out put like this. enter image description here

But I want like this. enter image description here

2
  • 1
    When you have any doubt regarding strings in php, take a look at this list of functions: php.net/manual/en/ref.strings.php . You'll find the function you are looking for, and you will learn about everything you can do with many others. Commented Oct 8, 2015 at 10:35
  • Try this html_entity_decode in place of mysql_escape_string. Commented Oct 8, 2015 at 10:51

4 Answers 4

2

try this

$dbstr = html_entity_decode("&lt;p&gt;Just install and use this app.&amp;nbsp;&lt;strong&gt;MENU -&amp;gt; SETTINGA&lt;/strong&gt;&lt;/p&gt;");
Sign up to request clarification or add additional context in comments.

Comments

1

try this

    $dbstr = htmlspecialchars_decode("<p>Just install and use this app. <strong>MENU -> SETTINGA</strong></p>");

echo $dbstr;

Comments

1

The reason why the tags get printed and not interpreted is, that with using the html special char coding (&lt; and &gt;) you are actually telling the browser that you want the special chars to be printed, not interpreted. Since you are retrieving this from a database, changing the actual content might not be the best idea or not even possible. Instead, you can decode the special chars with a built in php function:

$dbstr = htmlspecialchars_decode(mysql_escape_string("&lt;p&gt;Just install and use this app.&amp;nbsp;&lt;strong&gt;MENU -&amp;gt; SETTINGA&lt;/strong&gt;&lt;/p&gt;"));

Comments

1
$dbstr = html_entity_decode("&lt;p&gt;Just install and use this app.&amp;nbsp;&lt;strong&gt;MENU -&amp;gt; SETTINGA&lt;/strong&gt;&lt;/p&gt;");
echo $dbstr;

use html_entity_decode() function

html_entity_decode()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.