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

余生颓废 提交于 2019-12-04 01:09:43

问题


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

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

To render html just like:

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


回答1:


Use single quotes

Single quotes vs double quotes in PHP

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



回答2:


A. If you want to show the HTML Tags you can just use 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

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 html then you should use strip_tags

Example

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

Output

html


来源:https://stackoverflow.com/questions/12693093/do-not-escape-html-stored-as-string-execute-or-process-html-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!