PHP : Encoding specials symbols into HTML entities and Decoding HTML entities to special symbols with Example

In HTML normal special characters are considered as entities. So normally these are encoded as string. Normally &, <, >, ‘ and “ are special symbols. In PHP we can able to encode special symbols into HTML entity vice versa able to decode HTML entities into special symbols.

Encoding specials symbols into HTML entities

There is a PHP function htmlentities(). It will convert string with special character into HTML entity, It accepts String as argument and return Encoded string as Output.

Syntax:

htmlentities("STRING AS INPUT ARGUMENT");

Example:

$string = "The <strong> CodeExpert's </strong> is best site to learn concepts & technologies";
print $string;
// Output
The <strong> CodeExpert's </strong> is best site to learn concepts & technologies
$encodedString = htmlentities($string);
print $encodedString;
// Output
The <strong> CodeExpert's </strong> is best site to learn concepts & technologies

 

Decoding into HTML entities specials symbols

There is a PHP function htmlspecialchars_decode(). It will convert string with HTML entities into special character, It accepts String as argument and return Decoded string as Output.

Syntax:

htmlspecialchars_decode("STRING AS INPUT ARGUMENT");

Example:

$string = "The <strong> CodeExpert's </strong> is best site to learn concepts & technologies";
print $string;
// Output
The <strong> CodeExpert's </strong> is best site to learn concepts & technologies
$decodedString = htmlspecialchars_decode($string);
print $decodedString;
// Output
The <strong> CodeExpert's </strong> is best site to learn concepts & technologies
Category: