Sometimes you may want to encode certain characters in HTML, this can be done using their entity equivalents. The PHP htmlentities function will convert certain characters to their HTML entity equivalents.
The default encoding used in conversion is ISO-8859-1, this can be changed with the third function parameter.
Some characters have special use in HTML and will need to be encoded when used normally – an example of this is when showing HTML-source, where the less-than, and greater-than signs should be encoded. It can sometimes also be necessary to encode characters outside of the character set being used – if you are using ISO-8859-1, you won't directly be able to use "ÆØÅ", and they will either need to be encoded, or you need to move to another character set.
Encode characters with htmlentities
The below piece of PHP code will encode a string using the htmlentities function. We will later decode the same string using the html_ entity_ decode function.
$str = "A single word written in <b>bold</b>"; $str = htmlentities($str); echo $str;
The above was simple enough, but sometimes you may want to use another character set in the encoding process, this is done with the third parameter of the htmlentities function. The below script will leave double and single quotes unconverted, and use the UTF-8 charset instead of the default.
echo htmlentities($str, ENT_NOQUOTES, 'UTF-8');
Decoding characters with html_entity_decode
The html_entity_decode function is used to decode encoded strings. The below PHP script will decode the $str variable from the first script, using the default charset to begin with.
$decodedstr = htmlentities($str); echo $decodedstr;
If the string has been encoded using the UTF-8 character set, use the below in the decoding process:
$decodedstr = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8'); echo $decodedstr;