简体   繁体   English

如何将字符串转换为HTML字符串?

[英]How to convert string into HTML string?

I have a string(variable) having value of database cell. 我有一个具有数据库单元格值的字符串(变量)。 Actually that value is html code. 实际上,该值是html代码。 I have to print that code into html page. 我必须将该代码打印到html页面中。 I found that how to convert HTML string into DOM string. 我发现了如何将HTML字符串转换为DOM字符串。 But I don't know that How to convert string into HTML string. 但是我不知道如何将字符串转换为HTML字符串。 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. 我就这样出来了。 在此处输入图片说明

But I want like 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;");

try this 尝试这个

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

echo $dbstr;

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. 标签被打印而不被解释的原因是,使用html特殊字符编码( &lt;&gt; ),您实际上是在告诉浏览器您希望打印特殊字符,而不是被解释。 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: 相反,您可以使用内置的php函数解码特殊字符:

$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;"));
$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()函数

html_entity_decode() html_entity_decode()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM