English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP htmlspecialchars() function usage and examples

   PHP String 字符串函数手册

    The htmlspecialchars() function is used to convert special characters to HTML entities.

Syntax

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

Definition and usage

Used to convert special characters to HTML entities

Return value

It returns the converted string (string).
If the string contains an invalid code unit sequence in the specified encoding encoding, and no ENT_IGNORE or ENT_SUBSTITUTE flag is set, an empty string will be returned.

Predefined characters are:

  • & (ampersand) becomes &

  • " (double quote) becomes "

  • ' (single quote) becomes '

  • < (less than) becomes <

  • > (greater than) becomes >

注意:To convert special HTML entities back to characters, use htmlspecialchars_decode() Function.

Parameter

Serial numberParameters and descriptions
1

string

Required. It contains information about the input string

2

flags

Optional. Specify how to handle quotes, invalid encoding, and which document type to use.

Available quote types:

  • ENT_COMPAT - Default. Encode double quotes only.

  • ENT_QUOTES - Encode both double and single quotes.

  • ENT_NOQUOTES - Do not encode any quotes.

Invalid encoding:

  • ENT_IGNORE - Ignore invalid encoding instead of returning an empty string from the function. It should be avoided as it may have security implications.

  • ENT_SUBSTITUTE - Replace invalid encoding with a specified character containing the Unicode replacement character U+FFFD (UTF-8) or &#FFFD;, rather than returning an empty string.

  • ENT_DISALLOWED - Replace invalid code points in the specified document type with Unicode replacement character U+FFFD (UTF-8) or &#FFFD;.

Additional flags for specifying the document type:

  • ENT_HTML401 - 默认。作为 HTML 4.01 处理代码。

  • ENT_HTML5 - 作为 HTML 5 处理代码。

  • ENT_XML1 - 作为 XML 1 处理代码。

  • ENT_XHTML - 作为 XHTML 处理代码。

3

encoding

它是一个可选参数,定义了转换字符时使用的编码。

允许的值:

  • UTF-8 - 默认。ASCII 兼容多字节的 8 位 Unicode

  • ISO-8859-1 - 西欧

  • ISO-8859-15 - 西欧(加入欧元符号 + ISO-8859-1 中丢失的法语和芬兰语字母)

  • cp866 - DOS 专用 Cyrillic 字符集

  • cp1251 - Windows 专用 Cyrillic 字符集

  • cp1252 - Windows 专用西欧字符集

  • KOI8-R - 俄语

  • BIG5 - 繁体中文,主要在台湾使用

  • GB2312 - 简体中文,国家标准字符集

  • BIG5-HKSCS - 带香港扩展的 Big5

  • Shift_JIS - 日语

  • EUC-JP - 日语

  • MacRoman - Mac 操作系统使用的字符集

注意:在 PHP 5.4 更早版本,无法被识别的字符集将被忽略并由 ISO-8859-1 代替。自 PHP 5.4 起,无法被识别的字符集将被忽略并由 UTF-8 代替。

4

double_encode

一个指定了是否编码已存在的 HTML 实体的布尔值。
  • TRUE - 默认。将对每个实体进行转换。

  • FALSE - 不会对已存在的 HTML 实体进行编码。

在线示例

试试下面的实例,将预定义的字符转换为 HTML 实体:

<?php
   //将预定义的字符转换为 HTML 实体,编码双引号和单引号
   $input = htmlspecialchars("<a href='https://it.oldtoolbag.com'>w3codebox</a>", &ENT_QUOTES);
   echo &input;
?>
测试看看‹/›

输出结果-

<a href='https://it.oldtoolbag.com'>w3codebox</a>

PHP String 字符串函数手册