Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 391
john Avatar asked Oct 02 '12 15:10

john


People also ask

How do I escape HTML code?

Escape characters will always begin with the ampersand symbol (&) and end with a semicolon symbol (;). The characters in between the ampersand and semicolon make up the specific code name or number for a particular character.

Why do we escape HTML?

you have to escape html or xml when there is a possibility that it might get interpreted along with the page-generated html (read jsp). this good question also explains it. Save this answer.

How do you escape HTML in Java?

In Java, we can use Apache commons-text , StringEscapeUtils. escapeHtml4(str) to escape HTML characters. In the old days, we usually use the Apache commons-lang3 , StringEscapeUtils class to escape HTML, but this class is deprecated as of 3.6.

How do you replace HTML tag from string in Java?

The HTML tags can be removed from a given string by using replaceAll() method of String class. We can remove the HTML tags from a given string by using a regular expression. After removing the HTML tags from a string, it will return a string as normal text.


2 Answers

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 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
like image 148
Baba Avatar answered Sep 21 '22 18:09

Baba


Use single quotes

Single quotes vs double quotes in PHP

echo '<p>HTML</p>';
like image 40
csi Avatar answered Sep 21 '22 18:09

csi