Esercitazione PHP

PHP HOME Introduzione a PHP Installazione PHP Sintassi PHP Commenti PHP Variabili PHP PHP Eco/Stampa Tipi di dati PHP Stringhe PHP Numeri PHP PHP matematica Costanti PHP Operatori PHP PHP Se...Altro...Altro Passaggio PHP Ciclo PHP Funzioni PHP Matrici PHP Superglobali PHP RegEx PHP

Moduli PHP

Gestione dei moduli PHP Convalida del modulo PHP Modulo PHP richiesto URL/e-mail del modulo PHP Modulo PHP completo

PHP avanzato

Data e ora PHP PHP Include Gestione dei file PHP Apri/Leggi file PHP Creazione/scrittura di file PHP Caricamento file PHP Cookie PHP Sessioni PHP Filtri PHP Filtri PHP avanzati Funzioni di callback PHP PHP JSON Eccezioni PHP

PHP OOP

PHP Che cos'è OOP Classi/Oggetti PHP Costruttore PHP PHP distruttore Modificatori di accesso PHP Ereditarietà PHP Costanti PHP Classi astratte PHP Interfacce PHP Tratti PHP Metodi statici PHP Proprietà statiche PHP Spazi dei nomi PHP Iterabili PHP

Database MySQL

Database MySQL MySQL Connect MySQL Crea DB MySQL Crea tabella Dati di inserimento MySQL MySQL Ottieni l'ultimo ID MySQL inserisce più MySQL preparato MySQL Seleziona dati MySQL dove MySQL Ordina per MySQL Elimina dati Dati di aggiornamento MySQL Dati limite MySQL

PHP XML

Parser XML PHP Analizzatore PHP SimpleXML PHP SimpleXML - Ottieni PHP XML espatriato PHP XML DOM

PHP - AJAX

Introduzione all'Ajax AJAX PHP Database AJAX XML AJAX Ricerca in tempo reale AJAX Sondaggio AJAX

Esempi PHP

Esempi PHP compilatore PHP Quiz PHP Esercizi PHP Certificato PHP

Riferimento PHP

Panoramica di PHP matrice PHP Calendario PHP Data PHP Directory PHP Errore PHP Eccezione PHP File system PHP Filtro PHP PHP FTP PHP JSON Parole chiave PHP PHP Libxml Posta PHP PHP matematica PHP Varie PHP MySQLi Rete PHP Controllo dell'output PHP RegEx PHP PHP SimpleXML Flusso PHP Stringa PHP Gestione delle variabili PHP Analizzatore XML PHP Zip PHP Fuso orario PHP

PHP htmlcaratteri speciali() Funzione

❮ Riferimento alla stringa PHP

Esempio

Converti i caratteri predefiniti "<" (minore di) e ">" (maggiore di) in entità HTML:

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>

L'output HTML del codice sopra sarà (Visualizza sorgente):

<!DOCTYPE html>
<html>
<body>
This is some &lt;b&gt;bold&lt;/b&gt; text.
</body>
</html>

L'output del browser del codice sopra sarà:

This is some <b>bold</b> text.

Definizione e utilizzo

La funzione htmlspecialchars() converte alcuni caratteri predefiniti in entità HTML.

I caratteri predefiniti sono:

  • & (e commerciale) diventa &
  • " (virgolette doppie) diventa "
  • ' (virgoletta singola) diventa '
  • < (minore di) diventa <
  • > (maggiore di) diventa >

Suggerimento: per riconvertire entità HTML speciali in caratteri, utilizzare la funzione htmlspecialchars_decode() .


Sintassi

htmlspecialchars(string,flags,character-set,double_encode)

Valori dei parametri

Parameter Description
string Required. Specifies the string to convert
flags Optional. Specifies how to handle quotes, invalid encoding and the used document type.

The available quote styles are:

  • ENT_COMPAT - Default. Encodes only double quotes
  • ENT_QUOTES - Encodes double and single quotes
  • ENT_NOQUOTES - Does not encode any quotes

Invalid encoding:

  • ENT_IGNORE - Ignores invalid encoding instead of having the function return an empty string. Should be avoided, as it may have security implications.
  • ENT_SUBSTITUTE - Replaces invalid encoding for a specified character set with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD; instead of returning an empty string.
  • ENT_DISALLOWED - Replaces code points that are invalid in the specified doctype with a Unicode Replacement Character U+FFFD (UTF-8) or &#FFFD;

Additional flags for specifying the used doctype:

  • ENT_HTML401 - Default. Handle code as HTML 4.01
  • ENT_HTML5 - Handle code as HTML 5
  • ENT_XML1 - Handle code as XML 1
  • ENT_XHTML - Handle code as XHTML
character-set Optional. A string that specifies which character-set to use.

Allowed values are:

  • UTF-8 - Default. ASCII compatible multi-byte 8-bit Unicode
  • ISO-8859-1 - Western European
  • ISO-8859-15 - Western European (adds the Euro sign + French and Finnish letters missing in ISO-8859-1)
  • cp866 - DOS-specific Cyrillic charset
  • cp1251 - Windows-specific Cyrillic charset
  • cp1252 - Windows specific charset for Western European
  • KOI8-R - Russian
  • BIG5 - Traditional Chinese, mainly used in Taiwan
  • GB2312 - Simplified Chinese, national standard character set
  • BIG5-HKSCS - Big5 with Hong Kong extensions
  • Shift_JIS - Japanese
  • EUC-JP - Japanese
  • MacRoman - Character-set that was used by Mac OS

Note: Unrecognized character-sets will be ignored and replaced by ISO-8859-1 in versions prior to PHP 5.4. As of PHP 5.4, it will be ignored an replaced by UTF-8.

double_encode Optional. A boolean value that specifies whether to encode existing html entities or not.
  • TRUE - Default. Will convert everything
  • FALSE - Will not encode existing html entities


Dettagli tecnici

Valore di ritorno: Restituisce la stringa convertita

Se la stringa contiene una codifica non valida, restituirà una stringa vuota, a meno che non siano impostati i flag ENT_IGNORE o ENT_SUBSTITUTE
Versione PHP: 4+
Registro delle modifiche: PHP 5.6 - Modificato il valore predefinito per il parametro del set di caratteri nel valore del set di caratteri predefinito (nella configurazione).
PHP 5.4 - Modificato il valore predefinito per il parametro del set di caratteri in UTF-8.
PHP 5.4 - Aggiunti ENT_SUBSTITUTE, ENT_DISALLOWED, ENT_HTML401, ENT_HTML5, ENT_XML1 e ENT_XHTML
PHP 5.3 - Aggiunti ENT_IGNORE costante.
PHP 5.2.3 - Aggiunto il parametro double_encode .
PHP 4.1 - Aggiunto il parametro del set di caratteri .

Altri esempi

Esempio

Converti alcuni caratteri predefiniti in entità HTML:

<?php
$str = "Jane & 'Tarzan'";
echo htmlspecialchars($str, ENT_COMPAT); // Will only convert double quotes
echo "<br>";
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
echo "<br>";
echo htmlspecialchars($str, ENT_NOQUOTES); // Does not convert any quotes
?>

L'output HTML del codice sopra sarà (Visualizza sorgente):

<!DOCTYPE html>
<html>
<body>
Jane &amp; 'Tarzan'<br>
Jane &amp; &#039;Tarzan&#039;<br>
Jane &amp; 'Tarzan'
</body>
</html>

L'output del browser del codice sopra sarà:

Jane & 'Tarzan'
Jane & 'Tarzan'
Jane & 'Tarzan'

Esempio

Converti virgolette doppie in entità HTML:

<?php
$str = 'I love "PHP".';
echo htmlspecialchars($str, ENT_QUOTES); // Converts double and single quotes
?>

L'output HTML del codice sopra sarà (Visualizza sorgente):

<!DOCTYPE html>
<html>
<body>
I love &quot;PHP&quot;.
</body>
</html>

L'output del browser del codice sopra sarà:

I love "PHP".

❮ Riferimento alla stringa PHP