htmlentities (PHP 4, PHP 5)

Convert all applicable characters to HTML entities.

string htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = true ]]] )

This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities.
If you're wanting to decode instead (the reverse) you can use html_entity_decode().

HttpUtility.HtmlEncode Method (String) .NET Framework 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

Converts a string to an HTML-encoded string.

public static string HtmlEncode( string s )

HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. For example, when embedded in a block of text, the characters < and > are encoded as &lt; and &gt; for HTTP transmission.

next → ← prev

PHP htmlentities() function is string function, which is used to convert character to HTML entities.


String Specify the string to convert. required
flags Specify how to manage quotes, invalid encoding. optional
Character Specify the character set optional
Double_encode Specify a Boolean value. optional

Example 1


Go to

Example 2


Hello PHP : 'E=MC�'
Hello PHP : 'E=MC�'
Hello PHP : 'E=

Next TopicPHP String

← prev next →

For Videos Join Our Youtube Channel: Join Now


  • Send your Feedback to [email protected]

Help Others, Please Share

What is the easiest way to Html encode in PHP?

Somnath Muluk

52.5k34 gold badges216 silver badges224 bronze badges

asked Dec 9, 2009 at 13:13


By encode, do you mean: Convert all applicable characters to HTML entities?

htmlspecialchars or htmlentities

You can also use strip_tags if you want to remove all HTML tags :


Note: this will NOT stop all XSS attacks

answered Dec 9, 2009 at 13:16


1,39912 silver badges17 bronze badges



<h2>Encode HTML CODE</h2>

<form action='htmlencodeoutput.php' method='post'>
<textarea rows='30' cols='100'name='inputval'></textarea>
<input type='submit'>



 $fianlop=substr($spilt,'0', $sublen);

<textarea rows='20' cols='100'><?php echo $output?> </textarea> 

You can encode HTML like this .

answered Apr 25, 2015 at 4:28

Try this:

    $str = "This is some <b>bold</b> text.";
    echo htmlspecialchars($str);

answered Jul 18, 2017 at 12:01

Moby MMoby M

9001 gold badge7 silver badges26 bronze badges


I searched for hours, and I tried almost everything suggested.
This worked for almost every entity :

$input = "āžšķūņrūķīš ○ àéò ∀∂∋ ©€ ♣♦ ↠ ↔↛ ↙ ℜ℞";

echo htmlentities($input, ENT_HTML5  , 'UTF-8');

result :

&amacr;&zcaron;&scaron;&kcedil;&umacr;&ncedil;r&umacr;&kcedil;&imacr;&scaron; &cir; &agrave;&eacute;&ograve; &forall;&part;&ReverseElement; &copy;&euro; &clubs;&diamondsuit; &twoheadrightarrow; &harr;&nrarr; &swarr; &Rfr;&rx;rx;

answered Jan 18, 2019 at 16:21

Lu BlueLu Blue

1992 silver badges8 bronze badges

What are HTML entities?

An HTML entity is a piece of text ("string") that begins with an ampersand ( & ) and ends with a semicolon ( ; ). Entities are frequently used to display reserved characters (which would otherwise be interpreted as HTML code), and invisible characters (like non-breaking spaces).

What is HTML entity decode?

HtmlDecode(String) Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.

What is HTML special characters PHP?

Description. The htmlspecialchars() function is used to converts special characters ( e.g. & (ampersand), " (double quote), ' (single quote), < (less than), > (greater than)) to HTML entities ( i.e. & (ampersand) becomes &amp, ' (single quote) becomes &#039, < (less than) becomes &lt; (greater than) becomes &gt; ).

Are HTML entities necessary?

Generally, you don't need to use HTML entities if your editor supports Unicode. For some instances, entities can be useful: Your editor does not support Unicode. Your keyboard does not support the character you would like to type, such as em-dash or the copyright symbol.