Base de conhecimentoCentro de Exames
PT · PTPortuguês (Portugal)
Escolher idioma13

CKTIS / PHP

htmlspecialchars()

Codifica os carateres especiais de HTML ao inserir texto simples.

Versão: PHP 8.4; repository runtime, production runtime not attested

Sintaxe

htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string

Codifica os carateres especiais de HTML ao inserir texto simples. ENT_QUOTES inclui ambos os tipos de aspas e ENT_SUBSTITUTE substitui sequências inválidas. Indique a codificação real. Com double_encode=false as entidades reconhecidas ficam codificadas uma só vez.

Assinatura

htmlspecialchars(string $string, int $flags = ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML401, ?string $encoding = null, bool $double_encode = true): string

Saída do exemplo

A&B "Q" <tag> &

Exemplos

<?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;

Fontes