Page 114 - Open Soource Technologies 304.indd
P. 114
Web Technologies-I
Notes $string = <<< The_End “It is never going to work,” she cried, as she hit the backslash (\\) key.
The_End; echo addslashes($string); \”It\’s never going to work,\” she cried, as she hit the
backslash (\\) key. echo stripslashes($string); “It is never going to work,” she cried, as she hit
the backslash (\) key.
Some databases escape single quotes with another single quote instead of a backslash. For those
databases, enable magic_quotes_sybase in your php.ini file.
5.5.4 C-String Encoding
The addcslashes( ) function escapes arbitrary characters by placing backslashes before them. With
the exception of the characters in Table 5.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 5.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);
Example:
$string = stripcslashes(‘hello\tworld\n’); // $string is “hello\tworld\n”
5.6 Comparing Strings
PHP has two operators and six functions for comparing strings to each other.
5.6.1 Exact Comparisons
You can compare two strings for equality with the == and === operators. These operators differ
in how they deal with non-string operands. The ==operator casts non-string operands to strings,
so it reports that 3 and “3” are equal. The === operator does not cast, and returns false if the
types of the arguments differ.
108 LOVELY PROFESSIONAL UNIVERSITY