<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >html_entity_decode</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="String 字符串处理函数" HREF="ref.strings.html"><LINK REL="PREVIOUS" TITLE="hebrevc" HREF="function.hebrevc.html"><LINK REL="NEXT" TITLE="htmlentities" HREF="function.htmlentities.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.hebrevc.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.htmlentities.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.html-entity-decode" ></A >html_entity_decode</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN225116" ></A ><P > (PHP 4 >= 4.3.0, PHP 5)</P >html_entity_decode -- Convert all HTML entities to their applicable characters </DIV ><DIV CLASS="refsect1" ><A NAME="AEN225119" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >html_entity_decode</B > ( string string [, int quote_style [, string charset]] )<BR ></BR ><P > <B CLASS="function" >html_entity_decode()</B > is the opposite of <A HREF="function.htmlentities.html" ><B CLASS="function" >htmlentities()</B ></A > in that it converts all HTML entities to their applicable characters from <CODE CLASS="parameter" >string</CODE >. </P ><P > The optional second <CODE CLASS="parameter" >quote_style</CODE > parameter lets you define what will be done with 'single' and "double" quotes. It takes on one of three constants with the default being <TT CLASS="constant" ><B >ENT_COMPAT</B ></TT >: <DIV CLASS="table" ><A NAME="AEN225140" ></A ><P ><B >表 1. Available <CODE CLASS="parameter" >quote_style</CODE > constants</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><THEAD ><TR ><TH >Constant Name</TH ><TH >Description</TH ></TR ></THEAD ><TBODY ><TR ><TD ><TT CLASS="constant" ><B >ENT_COMPAT</B ></TT ></TD ><TD >Will convert double-quotes and leave single-quotes alone.</TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >ENT_QUOTES</B ></TT ></TD ><TD >Will convert both double and single quotes.</TD ></TR ><TR ><TD ><TT CLASS="constant" ><B >ENT_NOQUOTES</B ></TT ></TD ><TD >Will leave both double and single quotes unconverted.</TD ></TR ></TBODY ></TABLE ></DIV > </P ><P > The ISO-8859-1 character set is used as default for the optional third <CODE CLASS="parameter" >charset</CODE >. This defines the character set used in conversion. </P ><P > PHP 4.3.0 及其后续版本支持如下字符集。 <DIV CLASS="table" ><A NAME="AEN225164" ></A ><P ><B >表 2. 已支持字符集</B ></P ><TABLE BORDER="1" CLASS="CALSTABLE" ><COL><COL><COL><THEAD ><TR ><TH >字符集</TH ><TH >别名</TH ><TH >描述</TH ></TR ></THEAD ><TBODY ><TR ><TD >ISO-8859-1</TD ><TD >ISO8859-1</TD ><TD > 西欧,Latin-1 </TD ></TR ><TR ><TD >ISO-8859-15</TD ><TD >ISO8859-15</TD ><TD > 西欧,Latin-9。增加了 Latin-1(ISO-8859-1)中缺少的欧元符号、法国及芬兰字母。 </TD ></TR ><TR ><TD >UTF-8</TD ><TD > </TD ><TD > ASCII 兼容多字节 8-bit Unicode。 </TD ></TR ><TR ><TD >cp866</TD ><TD >ibm866, 866</TD ><TD > DOS-特有的 Cyrillic 字母字符集。PHP 4.3.2 开始支持该字符集。 </TD ></TR ><TR ><TD >cp1251</TD ><TD >Windows-1251, win-1251, 1251</TD ><TD > Windows-特有的 Cyrillic 字母字符集。PHP 4.3.2 开始支持该字符集。 </TD ></TR ><TR ><TD >cp1252</TD ><TD >Windows-1252, 1252</TD ><TD > Windows 对于西欧特有的字符集。 </TD ></TR ><TR ><TD >KOI8-R</TD ><TD >koi8-ru, koi8r</TD ><TD > 俄文。PHP 4.3.2 开始支持该字符集。 </TD ></TR ><TR ><TD >BIG5</TD ><TD >950</TD ><TD > 繁体中文,主要用于中国台湾。 </TD ></TR ><TR ><TD >GB2312</TD ><TD >936</TD ><TD > 简体中文,国际标准字符集。 </TD ></TR ><TR ><TD >BIG5-HKSCS</TD ><TD > </TD ><TD > 繁体中文,Big5 的延伸,主要用于香港。 </TD ></TR ><TR ><TD >Shift_JIS</TD ><TD >SJIS, 932</TD ><TD > 日文。 </TD ></TR ><TR ><TD >EUC-JP</TD ><TD >EUCJP</TD ><TD > 日文。 </TD ></TR ></TBODY ></TABLE ></DIV > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > ISO-8859-1 将代替任何其它无法识别的字符集。 </P ></BLOCKQUOTE ></DIV > </P ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > This function doesn't support multi-byte character sets in PHP < 5. </P ></BLOCKQUOTE ></DIV > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN225226" ></A ><P ><B >例 1. Decoding HTML entities</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br />$orig </font><font color="#007700">= </font><font color="#DD0000">"I'll \"walk\" the <b>dog</b> now"</font><font color="#007700">;<br /><br /></font><font color="#0000BB">$a </font><font color="#007700">= </font><font color="#0000BB">htmlentities</font><font color="#007700">(</font><font color="#0000BB">$orig</font><font color="#007700">);<br /><br /></font><font color="#0000BB">$b </font><font color="#007700">= </font><font color="#0000BB">html_entity_decode</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">);<br /><br />echo </font><font color="#0000BB">$a</font><font color="#007700">; </font><font color="#FF8000">// I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now<br /><br /></font><font color="#007700">echo </font><font color="#0000BB">$b</font><font color="#007700">; </font><font color="#FF8000">// I'll "walk" the <b>dog</b> now<br /><br /><br />// For users prior to PHP 4.3.0 you may do this:<br /></font><font color="#007700">function </font><font color="#0000BB">unhtmlentities</font><font color="#007700">(</font><font color="#0000BB">$string</font><font color="#007700">) <br />{<br /> </font><font color="#FF8000">// replace numeric entities<br /> </font><font color="#0000BB">$string </font><font color="#007700">= </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#DD0000">'~&#x([0-9a-f]+);~ei'</font><font color="#007700">, </font><font color="#DD0000">'chr(hexdec("\\1"))'</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">);<br /> </font><font color="#0000BB">$string </font><font color="#007700">= </font><font color="#0000BB">preg_replace</font><font color="#007700">(</font><font color="#DD0000">'~&#([0-9]+);~e'</font><font color="#007700">, </font><font color="#DD0000">'chr("\\1")'</font><font color="#007700">, </font><font color="#0000BB">$string</font><font color="#007700">);<br /> </font><font color="#FF8000">// replace literal entities<br /> </font><font color="#0000BB">$trans_tbl </font><font color="#007700">= </font><font color="#0000BB">get_html_translation_table</font><font color="#007700">(</font><font color="#0000BB">HTML_ENTITIES</font><font color="#007700">);<br /> </font><font color="#0000BB">$trans_tbl </font><font color="#007700">= </font><font color="#0000BB">array_flip</font><font color="#007700">(</font><font color="#0000BB">$trans_tbl</font><font color="#007700">);<br /> return </font><font color="#0000BB">strtr</font><font color="#007700">(</font><font color="#0000BB">$string</font><font color="#007700">, </font><font color="#0000BB">$trans_tbl</font><font color="#007700">);<br />}<br /><br /></font><font color="#0000BB">$c </font><font color="#007700">= </font><font color="#0000BB">unhtmlentities</font><font color="#007700">(</font><font color="#0000BB">$a</font><font color="#007700">);<br /><br />echo </font><font color="#0000BB">$c</font><font color="#007700">; </font><font color="#FF8000">// I'll "walk" the <b>dog</b> now<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > You might wonder why trim(html_entity_decode('&nbsp;')); doesn't reduce the string to an empty string, that's because the '&nbsp;' entity is not ASCII code 32 (which is stripped by <A HREF="function.trim.html" ><B CLASS="function" >trim()</B ></A >) but ASCII code 160 (0xa0) in the default ISO 8859-1 characterset. </P ></BLOCKQUOTE ></DIV > </P ><P > See also <A HREF="function.htmlentities.html" ><B CLASS="function" >htmlentities()</B ></A >, <A HREF="function.htmlspecialchars.html" ><B CLASS="function" >htmlspecialchars()</B ></A >, <A HREF="function.get-html-translation-table.html" ><B CLASS="function" >get_html_translation_table()</B ></A >, and <A HREF="function.urldecode.html" ><B CLASS="function" >urldecode()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.hebrevc.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.htmlentities.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >hebrevc</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >htmlentities</TD ></TR ></TABLE ></DIV ></BODY ></HTML >