php 不要转义存储为字符串的 html(执行或处理 html 字符串)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12693093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:02:47  来源:igfitidea点击:

Do not escape html stored as string (execute or process html string)

phphtmlescapingecho

提问by john

In PHP (Wordpress theme function, trying to add html stored in theme options to blog header), I'm trying to get the following line:

在 PHP(Wordpress 主题功能,尝试将存储在主题选项中的 html 添加到博客标题)中,我试图获得以下行:

$x="<p>html</p>"; echo $x;

To render html just like:

渲染 html 就像:

echo "<p>html</p>";

The results are different, the first one will display html tags while the second will process the html. Can someone please help. Thanks

结果不同,第一个将显示html标签,而第二个将处理html。有人可以帮忙吗。谢谢

采纳答案by csi

Use single quotes

使用单引号

Single quotes vs double quotes in PHP

PHP 中的单引号与双引号

echo '<p>HTML</p>';

回答by Baba

A. If you want to show the HTML Tags you can just use htmlentities

A. 如果你想显示 HTML 标签,你可以使用 htmlentities

Example

例子

$x = "<p>html</p>";
echo htmlentities($x);

Output

输出

<p>html</p>

B. If you want the the other way round its possible your string is stored as &lt;p&gt;html&lt;/p&gt;that is why you are seeing <p>html</p>then you should use html_entity_decode

B. 如果你想反过来,你的字符串被存储,因为&lt;p&gt;html&lt;/p&gt;这就是你看到的原因,<p>html</p>那么你应该使用html_entity_decode

Example

例子

$x = "&lt;p&gt;html&lt;/p&gt;";
echo html_entity_decode($x);

Output

输出

html

C. It could be you are not using a web broswer and you want htmlthen you should use strip_tags

C. 可能你没有使用网络浏览器,你想要html然后你应该使用strip_tags

Example

例子

$x = "<p>html</p>";
echo strip_tags($x);

Output

输出

html