PHP 8.3.4 Released!

addcslashes

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

addcslashesEsegue il quoting di una stringa con gli slash nello stile del C

Descrizione

addcslashes(string $str, string $charlist): string

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

Elenco dei parametri

str

La stringa su cui effettuare l'escape.

charlist

Una lista dei caratteri su cui effettuare l'escape. Se charlist 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 charlist assicurarsi di sapere quali caratteri sono presenti tra i caratteri che si sono impostati come inizio e la fine dell'intervallo.

<?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 sull'inizio, sulla fine e sui caratteri period ('.') sarà effettuato l'escape. Usare la funzione ord() per trovare il valore ASCII per un carattere.
<?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 charlist.

Valori restituiti

Restituisce la stringa con l'escape.

Log delle modifiche

Versione Descrizione
5.2.5 Sono state aggiunte le sequenze di escape \v e \f.

Esempi

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

Example #1 Esempio di addcslashes()

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

Vedere anche:

add a note

User Contributed Notes 6 notes

up
7
phpcoder at cyberpimp dot pimpdomain dot com
19 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
16 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
1
natNOSPAM at noworrie dot NO_SPAM dot com
21 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.
up
0
glitchmr at myopera dot com
10 years ago
If you need JS escaping function, use json_encode() instead.
up
-2
vishal dot ceeng at gmail dot com
4 years ago
echo addcslashes("zoo['.']", 'z..A');

Above code will create an error as per below

Invalid '..'-range, '..'-range needs to be incrementing -
up
-6
Johannes
16 years ago
Be carefull with adding the \ to the list of encoded characters. When you add it at the last position it encodes all encoding slashes. I got a lot of \\\ by this mistake.

So always encode \ at first.
To Top