Base de conocimientosCentro de exámenes
ESEspañol
Elegir idioma13

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

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

Resultado

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

Fuentes