PHP htmlentities() function

Example

Convert characters to HTML entities:

<?php
$str = "<? W3S?h????>";
echo htmlentities($str);
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
<© W3Sçh°°¦§>
</body>
</html>

Browser output of the above code:

<? W3S?h????>

Definition and Usage

The htmlentities() function converts characters to HTML entities.

Tip:To convert HTML entities back to characters, use html_entity_decode() function.

Tip:Please use get_html_translation_table() Function to return the translation table used by htmlentities().

Syntax

htmlentities(string,flags,character-set,double_encode)
Parameter Description
string Required. Specifies the string to be converted.
flags

Optional. Specifies how quotes, invalid encoding, and which document type are to be handled.

Available quote types:

  • ENT_COMPAT - Default. Encodes double quotes only.
  • ENT_QUOTES - Encodes both double and single quotes.
  • ENT_NOQUOTES - Does not encode any quotes.

Invalid encoding:

  • ENT_IGNORE - Ignores invalid encoding rather than returning an empty string. It should be avoided as it may have security implications.
  • ENT_SUBSTITUTE - Replaces invalid encoding with a specified character that includes the Unicode replacement character U+FFFD (UTF-8) or &#FFFD;, rather than returning an empty string.
  • ENT_DISALLOWED - Replaces invalid code points in the specified document type with the Unicode replacement character U+FFFD (UTF-8) or &#FFFD;.

Additional flags for specifying the document type used:

  • ENT_HTML401 - Default. Treats code as HTML 4.01.
  • ENT_HTML5 - Treats code as HTML 5.
  • ENT_XML1 - Treats code as XML 1.
  • ENT_XHTML - Treats code as XHTML.
character-set

Optional. A string that specifies the character set to be used.

Allowed values:

  • UTF-8 - Default. ASCII compatible 8-bit Unicode
  • ISO-8859-1 - Western Europe
  • ISO-8859-15 - Western Europe (including euro symbol + missing French and Finnish letters in ISO-8859-1)
  • cp866 - Cyrillic character set used by DOS
  • cp1251 - Cyrillic character set used by Windows
  • cp1252 - Western European character set used by Windows
  • KOI8-R - Russian
  • BIG5 - Traditional Chinese, mainly used in Taiwan
  • GB2312 - Simplified Chinese, national standard character set
  • BIG5-HKSCS - Big5 with Hong Kong extension
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Character set used by the Mac operating system

Note:In versions of PHP before 5.4, unrecognized character sets are ignored and replaced by ISO-8859-1. From PHP 5.4 onwards, unrecognized character sets are ignored and replaced by UTF-8.

double_encode

Optional. Boolean value, specifies whether to encode existing HTML entities.

  • TRUE - Default. Converts each entity.
  • FALSE - Will not encode existing HTML entities.

Technical details

Return value:

Return the converted string.

If string If the string contains invalid encoding, an empty string is returned unless ENT_IGNORE or ENT_SUBSTITUTE flags are set.

PHP version: 4+
Update log:

In PHP 5,character-set The default value of the parameter is changed to UTF-8.

In PHP 5.4, new features were added: ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1, and ENT_XHTML.

In PHP 5.3, a new feature was added: ENT_IGNORE.

In PHP 5.2.3, a new feature was added: double_encode Parameters.

In PHP 4.1, a new feature was added: character-set Parameters.

More examples

Example 1

Convert characters to HTML entities:

<?php
$str = "Bill & 'Steve'";
echo htmlentities($str, ENT_COMPAT); // Only convert double quotes
echo "<br>";
echo htmlentities($str, ENT_QUOTES); // Convert double and single quotes
echo "<br>";
echo htmlentities($str, ENT_NOQUOTES); // Do not convert any quotes
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
Bill & 'Steve'<br>
Bill & 'Tarzan'<br>
Bill & 'Steve'
</body>
</html>

Browser output of the above code:

Bill & 'Steve'
Bill & 'Steve'
Bill & 'Steve'

Run Instances

Example 2

Convert some characters to HTML entities using the Western European character set:

<?php
$str = "My name is Øyvind Øsane. I'm Norwegian.";
echo htmlentities($str, ENT_QUOTES, "ISO-8859-1"); 
// Will only convert double quotes (not single quotes), and uses the character-set Western European
?>

The HTML output of the above code is as follows (view source code):

<!DOCTYPE html>
<html>
<body>
My name is Øyvind Øsane. I'm Norwegian.
</body>
</html>

Browser output of the above code:

My name is Øyvind Øsane. I'm Norwegian.

Run Instances