(PHP 4, PHP 5, PHP 7, PHP 8)
addslashes — Esegue il quoting di una stringa con gli slash
La funzione restituisce una stringa con il carattere backslash anteposto ai caratteri che richiedono l'escape. Questi caratteri sono:
'
)"
)\
)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.
string
La stringa su cui fare l'escape.
Restituisce la stringa trasformata dopo l'escape.
Example #1 Un esempio di addslashes()
<?php
$str = "Is your name O'Reilly?";
// Outputs: Is your name O\'Reilly?
echo addslashes($str);
?>