CKTIS / PHP
htmlspecialchars()
Escapa caracteres especiales de HTML al insertar texto plano.
Versión: PHP 8.4; repository runtime, production runtime not attested
Sintaxis
htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): stringEscapa caracteres especiales de HTML al insertar texto plano. ENT_QUOTES incluye ambas comillas y ENT_SUBSTITUTE reemplaza secuencias no válidas. Indica la codificación real. Con double_encode=false las entidades reconocidas quedan codificadas una sola vez.
Firma
htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string
Salida del ejemplo
A&B "Q" <tag> &
Ejemplos
<?php
$text = "A&B \"Q\" <tag> &";
echo htmlspecialchars($text, ENT_QUOTES|ENT_SUBSTITUTE|ENT_HTML401,"UTF-8",false);
Resultado
A&B "Q" <tag> &