How to reverse htmlentities() / What is the opposite of htmlentities()?

PHP: How to reverse htmlentities() / What is the opposite of htmlentities()?

If you need to reverse or undo htmlentities() you can use the following function to do so in PHP:

  1. html_entity_decode($var)

This will convert HTML entities to their corresponding characters.

Info about html_entity_decode()

There are a few flags that you can use (as the 2nd parameter):

Constant NameDescription
ENT_COMPATWill convert double-quotes and leave single-quotes alone.
ENT_QUOTESWill convert both double and single quotes.
ENT_NOQUOTESWill leave both double and single quotes unconverted.
ENT_HTML401 Handle code as HTML 4.01.
ENT_XML1 Handle code as XML 1.
ENT_XHTML Handle code as XHTML.
ENT_HTML5 Handle code as HTML 5.

You can set what encoding to use by passing an option to the 3rd parameter. By default it will use the default_charset php setting.

The following character sets are supported:

ISO-8859-1ISO8859-1 Western European, Latin-1.
ISO-8859-5ISO8859-5 Little used cyrillic charset (Latin/Cyrillic).
ISO-8859-15ISO8859-15 Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1).
UTF-8  ASCII compatible multi-byte 8-bit Unicode.
cp866ibm866, 866 DOS-specific Cyrillic charset.
cp1251Windows-1251, win-1251, 1251 Windows-specific Cyrillic charset.
cp1252Windows-1252, 1252 Windows specific charset for Western European.
KOI8-Rkoi8-ru, koi8r Russian.
BIG5950 Traditional Chinese, mainly used in Taiwan.
GB2312936 Simplified Chinese, national standard character set.
BIG5-HKSCS  Big5 with Hong Kong extensions, Traditional Chinese.
Shift_JISSJIS, SJIS-win, cp932, 932 Japanese
EUC-JPEUCJP, eucJP-win Japanese
MacRoman  Charset that was used by Mac OS.
''  An empty string activates detection from script encoding (Zend multibyte), default_charset and current locale (see nl_langinfo() and setlocale()), in this order. Not recommended.
webdevetc profile pic

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This ( is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.


Comments and discussion about How to reverse htmlentities() / What is the opposite of htmlentities()?

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the PHP language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to convert between types in JS?

301 Redirect a single URL in .htaccess to a new location

How to increment a value in the database

How to view all installed PHP modules?

How to output (echo/print) content in PHP

How to exclude URLs from the Laravel CSRF protection?

How do you access the php://input stream?

How to select all rows for a certain month (or day, year or time), using Eloquent

How to set the Expires header in .htaccess

How to add a name prefix to all routes in a group?