CKTIS / PHP
htmlspecialchars()
Escape HTML-significant characters when inserting plain text into HTML.
Version: PHP 8.4; repository runtime, production runtime not attested
Syntax
htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): stringEscape HTML-significant characters when inserting plain text into HTML. ENT_QUOTES also escapes both quote characters and ENT_SUBSTITUTE replaces invalid input sequences. Use the actual character encoding. With double_encode false, recognized entities remain encoded once.
Signature
htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string
Example output
A&B "Q" <tag> &
Examples
<?php
$text = "A&B \"Q\" <tag> &";
echo htmlspecialchars($text, ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401,"UTF-8",false);
Result
A&B "Q" <tag> &