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
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.
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.
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.
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.
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 <p>html</p>
that is why you are seeing <p>html</p>
then you should use html_entity_decode
Example
$x = "<p>html</p>";
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
Use single quotes
Single quotes vs double quotes in PHP
echo '<p>HTML</p>';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With