<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Convert all applicable characters to HTML entities</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.html-entity-decode.html">html_entity_decode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.htmlspecialchars-decode.html">htmlspecialchars_decode</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.htmlentities" class="refentry"> <div class="refnamediv"> <h1 class="refname">htmlentities</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">htmlentities</span> — <span class="dc-title">Convert all applicable characters to HTML entities</span></p> </div> <div class="refsect1 description" id="refsect1-function.htmlentities-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>htmlentities</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$string</code></span> [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code><span class="initializer"> = ENT_COMPAT | ENT_HTML401</span></span> [, <span class="methodparam"><span class="type">string</span> <code class="parameter">$encoding</code><span class="initializer"> = 'UTF-8'</span></span> [, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$double_encode</code><span class="initializer"> = true</span></span> ]]] )</div> <p class="para rdfs-comment"> This function is identical to <span class="function"><a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a></span> in all ways, except with <span class="function"><strong>htmlentities()</strong></span>, all characters which have HTML character entity equivalents are translated into these entities. </p> <p class="para"> If you want to decode instead (the reverse) you can use <span class="function"><a href="function.html-entity-decode.html" class="function">html_entity_decode()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-function.htmlentities-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">string</code></em></span> <dd> <p class="para"> The input string. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">flags</code></em></span> <dd> <p class="para"> A bitmask of one or more of the following flags, which specify how to handle quotes, invalid code unit sequences and the used document type. The default is <em>ENT_COMPAT | ENT_HTML401</em>. <table class="doctable table"> <caption><strong>Available <em><code class="parameter">flags</code></em> constants</strong></caption> <thead> <tr> <th>Constant Name</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td><strong><code>ENT_COMPAT</code></strong></td> <td>Will convert double-quotes and leave single-quotes alone.</td> </tr> <tr> <td><strong><code>ENT_QUOTES</code></strong></td> <td>Will convert both double and single quotes.</td> </tr> <tr> <td><strong><code>ENT_NOQUOTES</code></strong></td> <td>Will leave both double and single quotes unconverted.</td> </tr> <tr> <td><strong><code>ENT_IGNORE</code></strong></td> <td> Silently discard invalid code unit sequences instead of returning an empty string. Using this flag is discouraged as it <a href="http://unicode.org/reports/tr36/#Deletion_of_Noncharacters" class="link external">» may have security implications</a>. </td> </tr> <tr> <td><strong><code>ENT_SUBSTITUTE</code></strong></td> <td> Replace invalid code unit sequences with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of returning an empty string. </td> </tr> <tr> <td><strong><code>ENT_DISALLOWED</code></strong></td> <td> Replace invalid code points for the given document type with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; (otherwise) instead of leaving them as is. This may be useful, for instance, to ensure the well-formedness of XML documents with embedded external content. </td> </tr> <tr> <td><strong><code>ENT_HTML401</code></strong></td> <td> Handle code as HTML 4.01. </td> </tr> <tr> <td><strong><code>ENT_XML1</code></strong></td> <td> Handle code as XML 1. </td> </tr> <tr> <td><strong><code>ENT_XHTML</code></strong></td> <td> Handle code as XHTML. </td> </tr> <tr> <td><strong><code>ENT_HTML5</code></strong></td> <td> Handle code as HTML 5. </td> </tr> </tbody> </table> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">encoding</code></em></span> <dd> <p class="para"> Like <span class="function"><a href="function.htmlspecialchars.html" class="function">htmlspecialchars()</a></span>, <span class="function"><strong>htmlentities()</strong></span> takes an optional third argument <em><code class="parameter">encoding</code></em> which defines encoding used in conversion. If omitted, the default value for this argument is ISO-8859-1 in versions of PHP prior to 5.4.0, and UTF-8 from PHP 5.4.0 onwards. Although this argument is technically optional, you are highly encouraged to specify the correct value for your code. </p> <p class="para"> The following character sets are supported: <table class="doctable table"> <caption><strong>Supported charsets</strong></caption> <thead> <tr> <th>Charset</th> <th>Aliases</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>ISO-8859-1</td> <td>ISO8859-1</td> <td> Western European, Latin-1. </td> </tr> <tr> <td>ISO-8859-5</td> <td>ISO8859-5</td> <td> Little used cyrillic charset (Latin/Cyrillic). </td> </tr> <tr> <td>ISO-8859-15</td> <td>ISO8859-15</td> <td> Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1 (ISO-8859-1). </td> </tr> <tr> <td>UTF-8</td> <td class="empty"> </td> <td> ASCII compatible multi-byte 8-bit Unicode. </td> </tr> <tr> <td>cp866</td> <td>ibm866, 866</td> <td> DOS-specific Cyrillic charset. </td> </tr> <tr> <td>cp1251</td> <td>Windows-1251, win-1251, 1251</td> <td> Windows-specific Cyrillic charset. </td> </tr> <tr> <td>cp1252</td> <td>Windows-1252, 1252</td> <td> Windows specific charset for Western European. </td> </tr> <tr> <td>KOI8-R</td> <td>koi8-ru, koi8r</td> <td> Russian. </td> </tr> <tr> <td>BIG5</td> <td>950</td> <td> Traditional Chinese, mainly used in Taiwan. </td> </tr> <tr> <td>GB2312</td> <td>936</td> <td> Simplified Chinese, national standard character set. </td> </tr> <tr> <td>BIG5-HKSCS</td> <td class="empty"> </td> <td> Big5 with Hong Kong extensions, Traditional Chinese. </td> </tr> <tr> <td>Shift_JIS</td> <td>SJIS, SJIS-win, cp932, 932</td> <td> Japanese </td> </tr> <tr> <td>EUC-JP</td> <td>EUCJP, eucJP-win</td> <td> Japanese </td> </tr> <tr> <td>MacRoman</td> <td class="empty"> </td> <td> Charset that was used by Mac OS. </td> </tr> <tr> <td><em>''</em></td> <td class="empty"> </td> <td> An empty string activates detection from script encoding (Zend multibyte), <a href="ini.core.html#ini.default-charset" class="link">default_charset</a> and current locale (see <span class="function"><a href="function.nl-langinfo.html" class="function">nl_langinfo()</a></span> and <span class="function"><a href="function.setlocale.html" class="function">setlocale()</a></span>), in this order. Not recommended. </td> </tr> </tbody> </table> <blockquote class="note"><p><strong class="note">Note</strong>: <span class="simpara"> Any other character sets are not recognized. The default encoding will be used instead and a warning will be emitted. </span> </p></blockquote> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">double_encode</code></em></span> <dd> <p class="para"> When <em><code class="parameter">double_encode</code></em> is turned off PHP will not encode existing html entities. The default is to convert everything. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.htmlentities-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the encoded string. </p> <p class="para"> If the input <em><code class="parameter">string</code></em> contains an invalid code unit sequence within the given <em><code class="parameter">encoding</code></em> an empty string will be returned, unless either the <strong><code>ENT_IGNORE</code></strong> or <strong><code>ENT_SUBSTITUTE</code></strong> flags are set. </p> </div> <div class="refsect1 changelog" id="refsect1-function.htmlentities-changelog"> <h3 class="title">Changelog</h3> <p class="para"> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.4.0</td> <td> The default value for the <em><code class="parameter">encoding</code></em> parameter was changed to UTF-8. </td> </tr> <tr> <td>5.4.0</td> <td> The constants <strong><code>ENT_SUBSTITUTE</code></strong>, <strong><code>ENT_DISALLOWED</code></strong>, <strong><code>ENT_HTML401</code></strong>, <strong><code>ENT_XML1</code></strong>, <strong><code>ENT_XHTML</code></strong> and <strong><code>ENT_HTML5</code></strong> were added. </td> </tr> <tr> <td>5.3.0</td> <td> The constant <strong><code>ENT_IGNORE</code></strong> was added. </td> </tr> <tr> <td>5.2.3</td> <td> The <em><code class="parameter">double_encode</code></em> parameter was added. </td> </tr> <tr> <td>4.1.0</td> <td> The <em><code class="parameter">encoding</code></em> parameter was added. </td> </tr> <tr> <td>4.0.3</td> <td> The <em><code class="parameter">flags</code></em> parameter was added. </td> </tr> </tbody> </table> </p> </div> <div class="refsect1 examples" id="refsect1-function.htmlentities-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4836"> <p><strong>Example #1 A <span class="function"><strong>htmlentities()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"A 'quote' is <b>bold</b>"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Outputs: A 'quote' is &lt;b&gt;bold&lt;/b&gt;<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Outputs: A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <div class="example" id="example-4837"> <p><strong>Example #2 Usage of <strong><code>ENT_IGNORE</code></strong></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"\x8F!!!"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// Outputs an empty string<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">, </span><span style="color: #DD0000">"UTF-8"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// Outputs "!!!"<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">htmlentities</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES </span><span style="color: #007700">| </span><span style="color: #0000BB">ENT_IGNORE</span><span style="color: #007700">, </span><span style="color: #DD0000">"UTF-8"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.htmlentities-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.html-entity-decode.html" class="function" rel="rdfs-seeAlso">html_entity_decode()</a> - Convert all HTML entities to their applicable characters</span></li> <li class="member"> <span class="function"><a href="function.get-html-translation-table.html" class="function" rel="rdfs-seeAlso">get_html_translation_table()</a> - Returns the translation table used by htmlspecialchars and htmlentities</span></li> <li class="member"> <span class="function"><a href="function.htmlspecialchars.html" class="function" rel="rdfs-seeAlso">htmlspecialchars()</a> - Convert special characters to HTML entities</span></li> <li class="member"> <span class="function"><a href="function.nl2br.html" class="function" rel="rdfs-seeAlso">nl2br()</a> - Inserts HTML line breaks before all newlines in a string</span></li> <li class="member"> <span class="function"><a href="function.urlencode.html" class="function" rel="rdfs-seeAlso">urlencode()</a> - URL-encodes string</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.html-entity-decode.html">html_entity_decode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.htmlspecialchars-decode.html">htmlspecialchars_decode</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>