Do not escape html stored as string (execute or pr

2019-02-17 06:26发布

问题:

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