phpday 2027 - Call for Papers

addcslashes

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

addcslashes — Esegue il quoting di una stringa con gli slash nello stile del C

Descrizione

function addcslashes(string $string, string $characters): string

La funzione restituisce una stringa con il carattere di backslash '\' anteposto ai caratteri che sono indicati nel parametro characters.

Elenco dei parametri

string

La stringa su cui effettuare l'escape.

characters

Una lista dei caratteri su cui effettuare l'escape. Se characters contiene i caratteri \n, \r ecc., essi sono convertiti nello stile C-like, mentre gli altri caratteri non alfanumerici con codici ASCII minori di 32 e maggiori di 126 sono convertiti nella rappresentazione ottale.

Quando si definisce una sequenza di caratteri nel parametro characters assicurarsi di sapere quali caratteri sono presenti tra i caratteri che si sono impostati come inizio e la fine dell'intervallo.

Example #1 addcslashes() con Intervalli

<?php
echo addcslashes('foo[ ]', 'A..z');
// output:  \f\o\o\[ \]
// Su tutte le lettere maiuscole e minuscole verrà effettuato l'escape
// ... ma anche [\]^_`
?>
Inoltre, se il primo carattere in un intervallo ha un valore ASCII maggiore del secondo carattere nell'intervallo, non verrà costruito nessun intervallo. Solo sui caratteri di inizio e di fine e sul punto ('.') sarà effettuato l'escape. Usare la funzione ord() per trovare il valore ASCII per un carattere.

Example #2 addcslashes() con Caratteri in Ordine Errato

<?php
echo addcslashes("zoo['.']", 'z..A');
// output:  \zoo['\.']
?>

Occorre prestare attenzione se si imposta di anteporre il carattere di escape ai caratteri 0, a, b, f, n, r, t e v. Questi saranno convertiti in \0, \a, \b, \f, \n, \r, \t e \v, i quali sono sequenze di escape predefinite in C. Molte di queste sequenze sono anche definite in altri linguaggi derivati da C, incluso PHP, il che significa che non si possono ottenere i risultati desiderati se si usa l'output di addcslashes() per generare codice in questi linguaggi con questi caratteri definiti in characters.

Valori restituiti

Restituisce la stringa con l'escape.

Esempi

Usare characters pari a "\0..\37", significa che si vuole effettuare l'escape di tutti i caratteri con codice ASCII tra 0 e 31.

Example #3 Esempio di addcslashes()

<?php
$not_escaped = "PHP compie\nTrent'anni!\tEvviva l'Elefante!\n";
$escaped = addcslashes($not_escaped, "\0..\37!@\177..\377");
echo $escaped;
?>

Vedere anche:

  • stripcslashes() - Rimuove il quoting da una stringa su cui è stato eseguito il quoting con addcslashes
  • stripslashes() - Rimuove i caratteri di escape da una stringa su cui si è effettuato l'escape
  • addslashes() - Esegue il quoting di una stringa con gli slash
  • htmlspecialchars() - Converte i caratteri speciali in entità HTML
  • quotemeta() - Esegue il quoting dei metacaratteri

+add a note

User Contributed Notes 3 notes

up
8
phpcoder at cyberpimp dot pimpdomain dot com
21 years ago
If you are using addcslashes() to encode text which is to later be decoded back to it's original form, you MUST specify the backslash (\) character in charlist!

Example:

<?php
  $originaltext = 'This text does NOT contain \\n a new-line!';
  $encoded = addcslashes($originaltext, '\\');
  $decoded = stripcslashes($encoded);
  //$decoded now contains a copy of $originaltext with perfect integrity
  echo $decoded; //Display the sentence with it's literal \n intact
?>

If the '\\' was not specified in addcslashes(), any literal \n (or other C-style special character) sequences in $originaltext would pass through un-encoded, but then be decoded into control characters by stripcslashes() and the data would lose it's integrity through the encode-decode transaction.
up
3
stein at visibone dot com
19 years ago
addcslashes() treats NUL as a string terminator:

   assert("any"  === addcslashes("any\0body", "-"));

unless you order it backslashified:

   assert("any\\000body" === addcslashes("any\0body", "\0"));

(Uncertain whether this should be declared a bug or simply that addcslashes() is not binary-safe, whatever that means.)
up
0
natNOSPAM at noworrie dot NO_SPAM dot com
24 years ago
I have found the following to be much more appropriate code example:

<?php
$escaped = addcslashes($not_escaped, "\0..\37!@\@\177..\377");
?>

This will protect original, innocent backslashes from stripcslashes.
To Top