Knowledge baseExam Center
ENEnglish
Choose language13

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): string

Escape 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> &amp;";
echo htmlspecialchars($text, ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401,"UTF-8",false);

Result

A&amp;B &quot;Q&quot; &lt;tag&gt; &amp;

Sources