How to display HTML tags as plain text using PHP
In this tutorial, we are going to see different methods to display HTML tags as plain text using PHP. HTML is a very useful language, but sometimes, on a website, it can be a problem. Especially if you use a script to parse plain text.
- Method 1: Using htmlspecialchars() function
- Method 2: Using htmlentities() function
How to display HTML tags as plain text using htmlspecialchars()
htmlspecialchars() function is a built-in PHP function used to convert all predefined characters into HTML entities.
<?php echo("<b>Without using htmlspecialchars() function</b><br>"); $str = htmlspecialchars("<b>Using htmlspecialchars() function</b>", ENT_QUOTES); echo($str); ?>
Output:
Without using htmlspecialchars() function <b>Using htmlspecialchars() function</b>
[st_adsense]
How to display HTML tags as plain text using htmlentities()
htmlentities() function is a built-in PHP function used to convert all predefined characters into HTML entities.
<?php echo("<b>Without using htmlentities() function</b><br>"); $str = htmlspecialchars("<b>Using htmlentities() function</b>", ENT_QUOTES); echo($str); ?>
Output:
Without using htmlentities() function <b>Using htmlentities() function</b>[st_adsense]