addslashes
PHP Manual
PHP Manual»String Funzioni»addslashes

addslashes

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

addslashesEsegue il quoting di una stringa con gli slash

Descrizione

function addslashes(string $string): 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) . "';");
?>

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

string

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:

To Top