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 kind of database field type should you (normally) use for foreign keys?

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

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

How to install and use optipng on Linux CentOS

What are some ways you can log debug messages and data in JS?

How to see all of the options for an artisan command?

How to block an IP range based on CIDR in Apache with .htaccess

How to set up an alias to map one directory to another destination

What is the difference between empty(), isset(), != null, is_null() in PHP

How to generate an array for a dropdown (with ['key' => 'value']) suitable for a dropdown, from a Laravel collection of Eloquent objects?