Because PHP programs often interact with HTML pages, web addresses
(URLs), and databases, there are functions to help you work with
those types of data. HTML, web page addresses, and database commands
are all strings, but they each require different characters to be
escaped in different ways. For instance, a space in a web address
must be written as %20, while a literal less-than sign (<) in an
HTML document must be written as <. PHP has a number of
built-in functions to convert to and from these encodings.
4.5.4. C-String Encoding
The addcslashes( ) function escapes
arbitrary characters by placing backslashes before them. With the
exception of the characters in Table 4-4, characters with ASCII values less than 32 or above 126 are encoded with their octal values
(e.g., "\002"). The addcslashes( ) and stripcslashes( ) functions are
used with nonstandard database systems that have their own ideas of
which characters need to be escaped.
Table 4-4. Single-character escapes recognized by addcslashes( ) and stripcslashes( )
ASCII value
|
Encoding
|
7
|
\a
|
8
|
\b
|
9
|
\t
|
10
|
\n
|
11
|
\v
|
12
|
\f
|
13
|
\r
|
Call addcslashes( ) with two arguments—the
string to encode and the characters to escape:
$escaped = addcslashes(string, charset);
Specify a range of characters to escape with the
".." construct:
echo addcslashes("hello\tworld\n", "\x00..\x1fz..\xff");
hello\tworld\n
Beware of specifying '0', 'a', 'b', 'f', 'n', 'r', 't', or 'v' in the
character set, as they will be turned into '\0', '\a', etc. These
escapes are recognized by C and PHP and may cause confusion.
stripcslashes( ) takes a string and returns a copy with the escapes
expanded:
$string = stripcslashes(escaped);
For example:
$string = stripcslashes('hello\tworld\n');
// $string is "hello\tworld\n"