Testing htmlspecialchars() PHP Function - Converting HTML Characters

When data needs to be presented in Web pages, HTML special characters must be well protected Otherwise, data well not be presented properly, or the Web will become corrupted. This page allows you to learn and test the htmlspecialchars() function to protect embedded data in Web pages.

What are HTML special characters?

Most of you probably know that there are 3 basic HTML special characters reserved for HTML tags and entities: (<), (>) and (&).

But there are 2 more characters reserved for quoting tag attributes: (") and (').

If you don't protect those characters, your Web page will not work. Try the following HTML code, and see what happens:

Question: is it true that 23:59 < 00:00?<br>
Answer: <input value='it's true.'>

How to use and test htmlspecialchars() PHP function?

htmlspecialchars(string) is the most commonly used PHP function to convert HTML special characters to safe formats:

  • '&' (ampersand) becomes '&amp;'
  • '"' (double quote) becomes '&quot;'
  • ''' (single quote) becomes '&#039;' only when ENT_QUOTES is set. Not a default behavior.
  • '<' (less than) becomes '&lt;'
  • '>' (greater than) becomes '&gt;'

To help you learn and test htmlspecialchars() function, FYIcenter.com has designed this online testing page. All you need to do is to enter the testing string in the form below and click the Start button. The converted string will be presented in the result area.


Popup Windows Tutorials

20 tutorials on different ways to create and test popup windows created in JavaScript: Page unload popups, Pop re-open on close, ...

200+ PHP Script Tutorials

Each tutorial answers one commonly asked PHP programming question with a short, but precise and clear PHP script.