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

What are the predefined variables in PHP?

How to return JSON data to the user in Laravel?

How to find the intersection (same values) from two arrays?

What is WordPress default Apache .htaccess rules?

How to change the order that migrations happen?

How to make Apache force download of files (in .htaccess)

How to sort an array of dates and times?

What are Template literals (Template strings) in JS?

How to review blog posts (written by other users) before they get published on your WordPress blog?

How to find the average value of items in a Laravel Collection?