function.htmlspecialchars.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 382 行
HTML
382 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Convert special characters to HTML entities</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.htmlspecialchars-decode.html">htmlspecialchars_decode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.implode.html">implode</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.htmlspecialchars" class="refentry"> <div class="refnamediv"> <h1 class="refname">htmlspecialchars</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">htmlspecialchars</span> — <span class="dc-title">Convert special characters to HTML entities</span></p> </div> <div class="refsect1 description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><b><b>htmlspecialchars</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$string</tt></span> [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$quote_style</tt></span> [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$charset</tt></span> [, <span class="methodparam"><span class="type">bool</span> <tt class="parameter">$double_encode</tt></span> ]]] )</div> <p class="para rdfs-comment"> Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. This function returns a string with some of these conversions made; the translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use <a href="function.htmlentities.html" class="function">htmlentities()</a> instead. </p> <p class="simpara"> This function is useful in preventing user-supplied text from containing HTML markup, such as in a message board or guest book application. </p> <p class="para"> The translations performed are: <ul class="itemizedlist"> <li class="listitem"> <span class="simpara"> '&' (ampersand) becomes '&amp;' </span> </li> <li class="listitem"> <span class="simpara"> '"' (double quote) becomes '&quot;' when <b><tt>ENT_NOQUOTES</tt></b> is not set. </span> </li> <li class="listitem"> <span class="simpara"> ''' (single quote) becomes '&#039;' only when <b><tt>ENT_QUOTES</tt></b> is set. </span> </li> <li class="listitem"> <span class="simpara"> '<' (less than) becomes '&lt;' </span> </li> <li class="listitem"> <span class="simpara"> '>' (greater than) becomes '&gt;' </span> </li> </ul> </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">string</tt></i></span> <dd> <p class="para"> The <a href="language.types.string.html" class="type string">string</a> being converted. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">quote_style</tt></i></span> <dd> <p class="para"> The optional second argument, <i><tt class="parameter">quote_style</tt></i>, tells the function what to do with single and double quote characters. The default mode, <b><tt>ENT_COMPAT</tt></b>, is the backwards compatible mode which only translates the double-quote character and leaves the single-quote untranslated. If <b><tt>ENT_QUOTES</tt></b> is set, both single and double quotes are translated and if <b><tt>ENT_NOQUOTES</tt></b> is set neither single nor double quotes are translated. </p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">charset</tt></i></span> <dd> <p class="para"> Defines character set used in conversion. The default character set is ISO-8859-1. </p> <p class="para"> Following character sets are supported in PHP 4.3.0 and later. <table border="5"> <caption><b>Supported charsets</b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Charset</th> <th colspan="1">Aliases</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">ISO-8859-1</td> <td colspan="1" rowspan="1" align="left">ISO8859-1</td> <td colspan="1" rowspan="1" align="left"> Western European, Latin-1 </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">ISO-8859-15</td> <td colspan="1" rowspan="1" align="left">ISO8859-15</td> <td colspan="1" rowspan="1" align="left"> Western European, Latin-9. Adds the Euro sign, French and Finnish letters missing in Latin-1(ISO-8859-1). </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">UTF-8</td> <td class="empty"> </td> <td colspan="1" rowspan="1" align="left"> ASCII compatible multi-byte 8-bit Unicode. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">cp866</td> <td colspan="1" rowspan="1" align="left">ibm866, 866</td> <td colspan="1" rowspan="1" align="left"> DOS-specific Cyrillic charset. This charset is supported in 4.3.2. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">cp1251</td> <td colspan="1" rowspan="1" align="left">Windows-1251, win-1251, 1251</td> <td colspan="1" rowspan="1" align="left"> Windows-specific Cyrillic charset. This charset is supported in 4.3.2. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">cp1252</td> <td colspan="1" rowspan="1" align="left">Windows-1252, 1252</td> <td colspan="1" rowspan="1" align="left"> Windows specific charset for Western European. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">KOI8-R</td> <td colspan="1" rowspan="1" align="left">koi8-ru, koi8r</td> <td colspan="1" rowspan="1" align="left"> Russian. This charset is supported in 4.3.2. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">BIG5</td> <td colspan="1" rowspan="1" align="left">950</td> <td colspan="1" rowspan="1" align="left"> Traditional Chinese, mainly used in Taiwan. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">GB2312</td> <td colspan="1" rowspan="1" align="left">936</td> <td colspan="1" rowspan="1" align="left"> Simplified Chinese, national standard character set. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">BIG5-HKSCS</td> <td class="empty"> </td> <td colspan="1" rowspan="1" align="left"> Big5 with Hong Kong extensions, Traditional Chinese. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">Shift_JIS</td> <td colspan="1" rowspan="1" align="left">SJIS, 932</td> <td colspan="1" rowspan="1" align="left"> Japanese </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">EUC-JP</td> <td colspan="1" rowspan="1" align="left">EUCJP</td> <td colspan="1" rowspan="1" align="left"> Japanese </td> </tr> </tbody> </colgroup> </table> <blockquote><p><b class="note">Note</b>: <span class="simpara"> Any other character sets are not recognized and ISO-8859-1 will be used instead. </span> </p></blockquote></p> </dd> </dt> <dt> <span class="term"><i><tt class="parameter">double_encode</tt></i></span> <dd> <p class="para"> When <i><tt class="parameter">double_encode</tt></i> 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"> <h3 class="title">Return Values</h3> <p class="para"> The converted <a href="language.types.string.html" class="type string">string</a>. </p> </div> <div class="refsect1 changelog"> <h3 class="title">ChangeLog</h3> <p class="para"> <table class="informaltable"> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Version</th> <th colspan="1">Description</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">5.2.3</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">double_encode</tt></i> parameter was added. </td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">4.1.0</td> <td colspan="1" rowspan="1" align="left"> The <i><tt class="parameter">charset</tt></i> parameter was added. </td> </tr> </tbody> </colgroup> </table> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 <b>htmlspecialchars()</b> example</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$new </span><span style="color: #007700">= </span><span style="color: #0000BB">htmlspecialchars</span><span style="color: #007700">(</span><span style="color: #DD0000">"<a href='test'>Test</a>"</span><span style="color: #007700">, </span><span style="color: #0000BB">ENT_QUOTES</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$new</span><span style="color: #007700">; </span><span style="color: #FF8000">// &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> </div> <div class="refsect1 notes"> <h3 class="title">Notes</h3> <blockquote><p><b class="note">Note</b>: Note that this function does not translate anything beyond what is listed above. For full entity translation, see <a href="function.htmlentities.html" class="function">htmlentities()</a>. <br /> </p></blockquote> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="function.get-html-translation-table.html" class="function" rel="rdfs-seeAlso">get_html_translation_table()</a></li> <li class="member"><a href="function.htmlspecialchars-decode.html" class="function" rel="rdfs-seeAlso">htmlspecialchars_decode()</a></li> <li class="member"><a href="function.strip-tags.html" class="function" rel="rdfs-seeAlso">strip_tags()</a></li> <li class="member"><a href="function.htmlentities.html" class="function" rel="rdfs-seeAlso">htmlentities()</a></li> <li class="member"><a href="function.nl2br.html" class="function" rel="rdfs-seeAlso">nl2br()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.htmlspecialchars-decode.html">htmlspecialchars_decode</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.implode.html">implode</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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?