PHP: Hypertext Preprocessor (original) (raw)
addcslashes
(PHP 4, PHP 5, PHP 7, PHP 8)
addcslashes — Quote string with slashes in a C style
Description
Parameters
string
The string to be escaped.
characters
A list of characters to be escaped. Ifcharacters
contains characters\n
, \r
etc., they are converted in C-like style, while other non-alphanumeric characters with ASCII codes lower than 32 and higher than 126 converted to octal representation.
When you define a sequence of characters in the characters
argument make sure that you know what characters come between the characters that you set as the start and end of the range.
Example #1 addcslashes() with Ranges
<?php echo addcslashes('foo[ ]', 'A..z'); // output: \f\o\o\[ \] // All upper and lower-case letters will be escaped // ... but so will the [\]^_` ?>
Also, if the first character in a range has a higher ASCII value than the second character in the range, no range will be constructed. Only the start, end and period characters will be escaped. Use the ord() function to find the ASCII value for a character.
Example #2 addcslashes() with Characters in Wrong Order
<?php echo addcslashes("zoo['.']", 'z..A'); // output: \zoo['\.'] ?>
Be careful if you choose to escape characters 0, a, b, f, n, r, t and v. They will be converted to \0, \a, \b, \f, \n, \r, \t and \v, all of which are predefined escape sequences in C. Many of these sequences are also defined in other C-derived languages, including PHP, meaning that you may not get the desired result if you use the output ofaddcslashes() to generate code in those languages with these characters defined in characters
.
Return Values
Returns the escaped string.
Examples
characters
like "\0..\37", which would escape all characters with ASCII code between 0 and 31.
Example #3 addcslashes() example
<?php $not_escaped = "PHP isThirty\nYears Old!\tYay to the Elephant!\n"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>e</mi><mi>s</mi><mi>c</mi><mi>a</mi><mi>p</mi><mi>e</mi><mi>d</mi><mo>=</mo><mi>a</mi><mi>d</mi><mi>d</mi><mi>c</mi><mi>s</mi><mi>l</mi><mi>a</mi><mi>s</mi><mi>h</mi><mi>e</mi><mi>s</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">escaped = addcslashes(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">esc</span><span class="mord mathnormal">a</span><span class="mord mathnormal">p</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">a</span><span class="mord mathnormal">dd</span><span class="mord mathnormal">cs</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">a</span><span class="mord mathnormal">s</span><span class="mord mathnormal">h</span><span class="mord mathnormal">es</span><span class="mopen">(</span></span></span></span>not_escaped, "\0..\37!@\177..\377"); echo $escaped; ?>
See Also
- stripcslashes() - Un-quote string quoted with addcslashes
- stripslashes() - Un-quotes a quoted string
- addslashes() - Quote string with slashes
- htmlspecialchars() - Convert special characters to HTML entities
- quotemeta() - Quote meta characters
Found A Problem?
phpcoder at cyberpimp dot pimpdomain dot com ¶
20 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:
encoded=addcslashes(encoded = addcslashes(encoded=addcslashes(originaltext, '\\'); decoded=stripcslashes(decoded = stripcslashes(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.
`
17 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.)
`
natNOSPAM at noworrie dot NO_SPAM dot com ¶
23 years ago
`I have found the following to be much more appropriate code example:
escaped=addcslashes(escaped = addcslashes(escaped=addcslashes(not_escaped, "\0..\37!@\@\177..\377"); ?>This will protect original, innocent backslashes from stripcslashes.
`