函数名称:htmlspecialchars()
函数说明:htmlspecialchars() 函数用于将特殊字符转换为 HTML 实体,以防止 XSS(跨站脚本攻击)攻击。
函数语法:htmlspecialchars(string $string, int $flags = ENT_COMPAT | ENT_HTML401, string|null $encoding = null, bool $double_encode = true): string
参数说明:
返回值:返回转换后的字符串。
示例:
$str = 'This is a "quote" <tag>.';
$encoded_str = htmlspecialchars($str);
echo $encoded_str;
// 输出:This is a "quote" <tag>.
$str = 'This is a "quote" <tag>.';
$encoded_str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
echo $encoded_str;
// 输出:This is a "quote" <tag>.
$str = 'This is a "quote" <tag>.';
$encoded_str = htmlspecialchars($str, ENT_QUOTES, 'UTF-8', false);
echo $encoded_str;
// 输出:This is a "quote" <tag>.
注意事项: