htmlspecialchars

(PHP 4, PHP 5, PHP 7)

htmlspecialchars锟斤拷锟斤拷锟斤拷锟街凤拷转锟斤拷为 HTML 实锟斤拷

说锟斤拷

htmlspecialchars ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = TRUE ]]] ) : string

某锟斤拷锟街凤拷锟斤拷 HTML 锟斤拷锟斤拷锟斤拷锟斤拷锟矫达拷锟斤拷锟斤拷锟借保锟斤拷原锟解,锟斤拷要锟斤拷 HTML 实锟斤拷锟斤拷锟斤拷铩� 锟斤拷锟斤拷锟斤拷锟结返锟斤拷锟街凤拷转锟斤拷锟侥憋拷铩� 锟斤拷锟斤拷转锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟叫癸拷锟斤拷锟斤拷锟斤拷锟斤拷实锟藉,使锟斤拷 htmlentities() 锟斤拷锟芥本锟斤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷锟秸碉拷锟侥碉拷锟斤拷一锟铰的o拷锟斤拷锟矫猴拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷屎暇锟斤拷锟斤拷锟斤拷 HTML 锟侥碉拷锟斤拷锟斤拷锟斤拷 然锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷瞻锟斤拷锟斤拷址锟斤拷锟斤拷牡锟斤拷遣锟揭伙拷锟斤拷模锟� 锟斤拷要锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟街伙拷锟斤拷锟斤拷实锟斤拷锟斤拷锟绞斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷约锟� htmlentities() 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷实锟斤拷锟接︼拷锟斤拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷懿锟斤拷锟斤拷谩锟� 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟绞癸拷锟� mb_encode_numericentity() 锟斤拷锟芥。

执锟斤拷转锟斤拷
锟街凤拷 锟芥换锟斤拷
& (& 锟斤拷锟斤拷) &
" (双锟斤拷锟斤拷) "锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 ENT_NOQUOTES
' (锟斤拷锟斤拷锟斤拷) 锟斤拷锟斤拷锟斤拷 ENT_QUOTES 锟斤拷 ' (锟斤拷锟斤拷锟� ENT_HTML401) 锟斤拷锟斤拷锟斤拷 ' (锟斤拷锟斤拷锟� ENT_XML1锟斤拷 ENT_XHTML 锟斤拷 ENT_HTML5)锟斤拷
< (小锟斤拷) &lt;
> (锟斤拷锟斤拷) &gt;

锟斤拷锟斤拷

string

锟斤拷转锟斤拷锟斤拷 string锟斤拷

flags

位锟斤拷锟诫,锟斤拷锟斤拷锟斤拷某锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟缴o拷锟斤拷锟斤拷转锟藉处锟斤拷细锟节★拷锟斤拷效锟斤拷元锟斤拷锟叫★拷锟侥碉拷锟斤拷锟酵★拷 默锟斤拷锟斤拷 ENT_COMPAT | ENT_HTML401锟斤拷

锟斤拷效锟斤拷 flags 锟斤拷锟斤拷
锟斤拷锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷
ENT_COMPAT 锟斤拷转锟斤拷双锟斤拷锟脚o拷锟斤拷转锟斤拷锟斤拷锟斤拷锟脚★拷
ENT_QUOTES 锟斤拷转锟斤拷双锟斤拷锟斤拷也转锟斤拷锟斤拷锟斤拷锟脚★拷
ENT_NOQUOTES 锟斤拷/双锟斤拷锟脚讹拷锟斤拷转锟斤拷
ENT_IGNORE 锟斤拷默锟斤拷锟斤拷锟斤拷效锟侥达拷锟诫单元锟斤拷锟叫o拷锟斤拷锟斤拷锟角凤拷锟截匡拷锟街凤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷使锟矫此憋拷牵锟� 锟斤拷为锟斤拷» 锟斤拷锟斤拷锟叫帮拷全影锟斤拷锟斤拷
ENT_SUBSTITUTE 锟芥换锟斤拷效锟侥达拷锟诫单元锟斤拷锟斤拷为 Unicode 锟斤拷锟斤拷锟斤拷锟絉eplacement Character锟斤拷锟斤拷 U+FFFD (UTF-8) 锟斤拷锟斤拷 &#xFFFD; (锟斤拷锟斤拷)锟斤拷锟斤拷锟斤拷锟角凤拷锟截匡拷锟街凤拷锟斤拷锟斤拷
ENT_DISALLOWED 为锟侥碉拷锟斤拷锟斤拷效锟斤拷锟斤拷锟斤拷婊晃� Unicode 锟斤拷锟斤拷锟斤拷锟絉eplacement Character锟斤拷锟斤拷 U+FFFD (UTF-8)锟斤拷锟斤拷 &#xFFFD;锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟角帮拷锟斤拷锟斤拷锟斤拷锟斤拷原锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷戮秃锟斤拷锟斤拷茫锟揭拷锟街� XML 锟侥碉拷嵌锟斤拷锟斤拷锟斤拷锟斤拷锟绞憋拷锟绞斤拷戏锟斤拷锟�
ENT_HTML401 锟斤拷 HTML 4.01 锟斤拷锟斤拷锟斤拷搿�
ENT_XML1 锟斤拷 XML 1 锟斤拷锟斤拷锟斤拷搿�
ENT_XHTML 锟斤拷 XHTML 锟斤拷锟斤拷锟斤拷搿�
ENT_HTML5 锟斤拷 HTML 5 锟斤拷锟斤拷锟斤拷搿�

encoding

An optional argument defining the encoding used when converting characters.

If omitted, the default value of the encoding varies depending on the PHP version in use. In PHP 5.6 and later, the default_charset configuration option is used as the default value. PHP 5.4 and 5.5 will use UTF-8 as the default. Earlier versions of PHP use ISO-8859-1.

Although this argument is technically optional, you are highly encouraged to specify the correct value for your code if you are using PHP 5.5 or earlier, or if your default_charset configuration option may be set incorrectly for the given input.

锟斤拷锟斤拷锟斤拷使锟斤拷效锟斤拷锟较o拷锟斤拷锟� string 锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷效锟侥o拷 ISO-8859-1锟斤拷 ISO-8859-15锟斤拷 UTF-8锟斤拷 cp866锟斤拷 cp1251锟斤拷 cp1252锟斤拷 KOI8-R 锟斤拷锟斤拷锟斤拷锟斤拷同锟斤拷效锟斤拷锟斤拷 也锟斤拷锟斤拷说锟斤拷锟斤拷锟斤拷些锟斤拷锟斤拷锟斤, 锟斤拷 htmlspecialchars() 影锟斤拷锟斤拷址锟斤拷锟秸硷拷锟斤拷锟酵拷锟轿伙拷谩锟�

支锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷

支锟街碉拷锟街凤拷锟斤拷锟叫憋拷
锟街凤拷锟斤拷 锟斤拷锟斤拷 锟斤拷锟斤拷
ISO-8859-1 ISO8859-1 锟斤拷欧锟斤拷Latin-1
ISO-8859-5 ISO8859-5 Little used cyrillic charset (Latin/Cyrillic).
ISO-8859-15 ISO8859-15 锟斤拷欧锟斤拷Latin-9锟斤拷锟斤拷锟斤拷欧元锟斤拷锟脚o拷锟斤拷锟斤拷头锟斤拷锟斤拷锟斤拷锟侥革拷锟� Latin-1(ISO-8859-1) 锟斤拷缺失锟斤拷
UTF-8   ASCII 锟斤拷锟捷的讹拷锟街斤拷 8 位 Unicode锟斤拷
cp866 ibm866, 866 DOS 锟斤拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟斤拷搿o拷锟斤拷址锟斤拷锟斤拷锟� 4.3.2 锟芥本锟叫得碉拷支锟街★拷
cp1251 Windows-1251, win-1251, 1251 Windows 锟斤拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟斤拷搿o拷锟斤拷址锟斤拷锟斤拷锟� 4.3.2 锟芥本锟叫得碉拷支锟街★拷
cp1252 Windows-1252, 1252 Windows 锟斤拷锟叫碉拷锟斤拷欧锟斤拷锟诫。
KOI8-R koi8-ru, koi8r 锟斤拷锟斤。锟斤拷锟街凤拷锟斤拷锟斤拷 4.3.2 锟芥本锟叫得碉拷支锟街★拷
BIG5 950 锟斤拷锟斤拷锟斤拷锟侥o拷锟斤拷要锟斤拷锟斤拷锟叫癸拷台锟斤拷省锟斤拷
GB2312 936 锟斤拷锟斤拷锟斤拷锟侥o拷锟叫癸拷锟斤拷锟揭憋拷准锟街凤拷锟斤拷锟斤拷
BIG5-HKSCS   锟斤拷锟斤拷锟斤拷锟侥o拷锟斤拷锟斤拷锟斤拷锟斤拷锟秸癸拷锟� Big5 锟街凤拷锟斤拷锟斤拷
Shift_JIS SJIS, 932 锟斤拷锟斤拷
EUC-JP EUCJP 锟斤拷锟斤拷
MacRoman   Mac OS 使锟矫碉拷锟街凤拷锟斤拷锟斤拷
''   An empty string activates detection from script encoding (Zend multibyte), default_charset and current locale (see nl_langinfo() and setlocale()), in this order. Not recommended.

Note: 锟斤拷锟斤拷锟街凤拷锟斤拷没锟斤拷锟较可★拷锟斤拷锟斤拷使锟斤拷默锟较憋拷锟诫并锟阶筹拷锟届常锟斤拷

double_encode

锟截憋拷 double_encode 时锟斤拷PHP 锟斤拷锟斤拷转锟斤拷锟斤拷锟叫碉拷 HTML 实锟藉, 默锟斤拷锟斤拷全锟斤拷转锟斤拷锟斤拷

锟斤拷锟斤拷值

转锟斤拷锟斤拷锟� string锟斤拷

锟斤拷锟街革拷锟斤拷谋锟斤拷锟� encoding 锟斤, string 锟斤拷锟斤拷锟斤拷锟斤拷效锟侥达拷锟诫单元锟斤拷锟叫o拷 没锟斤拷锟斤拷锟斤拷 ENT_IGNORE 锟斤拷锟斤拷 ENT_SUBSTITUTE 锟斤拷堑锟斤拷锟斤拷锟铰o拷锟结返锟截匡拷锟街凤拷锟斤拷锟斤拷

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.6.0 The default value for the encoding parameter was changed to be the value of the default_charset configuration option.
5.4.0 encoding 锟斤拷锟斤拷锟斤拷默锟斤拷值锟侥筹拷 UTF-8锟斤拷
5.4.0 锟斤拷锟接筹拷锟斤拷 ENT_SUBSTITUTE锟斤拷 ENT_DISALLOWED锟斤拷 ENT_HTML401锟斤拷 ENT_XML1锟斤拷 ENT_XHTML锟斤拷 ENT_HTML5锟斤拷
5.3.0 锟斤拷锟接筹拷锟斤拷 ENT_IGNORE锟斤拷
5.2.3 锟斤拷锟接诧拷锟斤拷 double_encode锟斤拷

锟斤拷锟斤拷

Example #1 htmlspecialchars() 锟斤拷锟斤拷

<?php
$new 
htmlspecialchars("<a href='test'>Test</a>"ENT_QUOTES);
echo 
$new// &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
?>

注锟斤拷

Note:

注锟解,锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷转锟斤拷锟斤拷锟斤拷锟叫憋拷锟斤拷锟斤拷锟绞碉拷濉� 锟斤拷锟斤拷转锟斤拷锟斤拷渭锟� htmlentities()锟斤拷

Note:

锟斤拷锟� flags 锟斤拷锟斤拷锟斤拷模锟斤拷锟阶伙拷锟斤拷锟斤拷锟斤拷锟斤拷循锟斤拷锟铰癸拷锟斤拷

  • 锟斤拷 ENT_COMPAT锟斤拷ENT_QUOTES锟斤拷ENT_NOQUOTES 锟斤拷没锟斤拷锟矫o拷 默锟较撅拷锟斤拷 ENT_COMPAT锟斤拷
  • 锟斤拷锟斤拷锟斤拷貌锟街挂伙拷锟� ENT_COMPAT锟斤拷 ENT_QUOTES锟斤拷 ENT_NOQUOTES 锟斤拷锟斤拷锟饺硷拷锟斤拷叩锟斤拷锟� ENT_QUOTES锟斤拷 锟斤拷锟斤拷锟� ENT_COMPAT锟斤拷
  • 锟斤拷 ENT_HTML401锟斤拷 ENT_HTML5锟斤拷 ENT_XHTML锟斤拷 ENT_XML1 锟斤拷没锟斤拷锟矫o拷默锟斤拷锟斤拷 ENT_HTML401锟斤拷
  • 锟斤拷锟斤拷锟斤拷貌锟街挂伙拷锟� ENT_HTML401锟斤拷 ENT_HTML5锟斤拷 ENT_XHTML锟斤拷 ENT_XML1锟斤拷 锟斤拷锟饺硷拷锟斤拷叩锟斤拷锟� ENT_HTML5 锟斤拷锟斤拷锟� ENT_XHTML 锟斤拷 ENT_HTML401锟斤拷
  • 锟斤拷锟斤拷锟斤拷貌锟街挂伙拷锟� ENT_DISALLOWED锟斤拷 ENT_IGNORE锟斤拷 ENT_SUBSTITUTE锟斤拷锟斤拷锟饺硷拷锟斤拷叩锟斤拷锟� ENT_IGNORE锟斤拷 锟斤拷锟斤拷锟� ENT_SUBSTITUTE锟斤拷

锟轿硷拷

  • get_html_translation_table() - 锟斤拷锟斤拷使锟斤拷 htmlspecialchars 锟斤拷 htmlentities 锟斤拷锟阶拷锟斤拷锟�
  • htmlspecialchars_decode() - 锟斤拷锟斤拷锟斤拷锟� HTML 实锟斤拷转锟斤拷锟斤拷锟斤拷通锟街凤拷
  • strip_tags() - 锟斤拷锟街凤拷锟斤拷锟斤拷去锟斤拷 HTML 锟斤拷 PHP 锟斤拷锟�
  • htmlentities() - 锟斤拷锟街凤拷转锟斤拷为 HTML 转锟斤拷锟街凤拷
  • nl2br() - 锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷之前锟斤拷锟斤拷 HTML 锟斤拷锟叫憋拷锟�