addslashes

(PHP 4, PHP 5, PHP 7, PHP 8)

addslashesEsegue il quoting di una stringa con gli slash

Descrizione

addslashes(string $str): string

La funzione restituisce una stringa con il carattere backslash anteposto ai caratteri che richiedono l'escape. Questi caratteri sono:

  • virgoletta singola (')
  • doppia virgoletta (")
  • backslash (\)
  • NUL (il byte NUL)

Un caso d'uso di addslashes() è fare l'escape dei suddetti caratteri in una stringa che deve essere eseguita come script da PHP:

<?php
$str
= "O'Reilly?";
eval(
"echo '" . addslashes($str) . "';");
?>

Prima di PHP 5.4.0, la direttiva PHP magic_quotes_gpc era on per impostazione predefinita ed essenzialmente eseguiva addslashes() su tutti i dati GET, POST e COOKIE. addslashes() non deve essere usato su stringhe su cui è già stato effettuato l'escape con magic_quotes_gpc, poiché sulle stringhe verrebbe fatto un doppio escape. get_magic_quotes_gpc() può essere usata per controllare se magic_quotes_gpc è on.

A volte addslashes() è erroneamente utilizzato per provare a prevenire SQL Injection. Invece, dovrebbero essere usate funzioni di escape specifiche per il database e/o prepared statement.

Elenco dei parametri

str

La stringa su cui fare l'escape.

Valori restituiti

Restituisce la stringa trasformata dopo l'escape.

Esempi

Example #1 Un esempio di addslashes()

<?php
$str
= "Is your name O'Reilly?";

// Outputs: Is your name O\'Reilly?
echo addslashes($str);
?>

Vedere anche: