Web pages are made up of HTML code, so if you post or write some HTML code in a web pages, a browser would generally consider them normal code. Instead of showing them as they are literally, they have become part of the underlying HTML structure of that page.
Now how to address this problem and display HTML code and HTML tags correctly in a web page as the page content?
The answer is, use HTML entities.
HTML entities are a way to encode special HTML characters such as < and > into literal characters that cannot be interpreted by browsers as HTML code. Refer to the most common HTML entities conversion table below:
- ‘&’ (ampersand) becomes ‘&‘
- ‘"’ (double quote) becomes ‘"‘
- ”’ (single quote) becomes ‘'‘
- ‘<’ (less than) becomes ‘<‘
- ‘>’ (greater than) becomes ‘>‘
Therefore, to display HTML code as they are and show them to your web site visitors, write the special characters such as < and > in HTML entities. For example,
Has to be written:
There are of course handy tools you can use to make these conversions much easier. PHP has a function, htmlentities() that accepts normal HTML code and converts them into HTML entities. You can also rely on online tools to make that happen.
You should also read:
- PHP: Difference between htmlspecialchars() and htmlentities() Functions
- vBulletin: How to add more buttons to quick reply box toolbar?
- jQuery: Selecting elements with uncommon / special characters in ID or class name
- CSS Definition: What is CSS Defined and Explained
- PHP: Generating Summary Abstract from A Text or HTML String, Limiting by Words or Sentences