1--TEST--
2Test addslashes() function : usage variations - strings with characters to be backslashed
3--FILE--
4<?php
5/*
6 * Test addslashes() with various strings containing characters that can be backslashed
7*/
8
9echo "*** Testing addslashes() : with various strings containing characters to be backslashed ***\n";
10
11// initialising a heredoc string
12$heredoc_string = <<<EOT
13This is line 1 of 'heredoc' string
14This is line 2 of "heredoc" string
15EOT;
16
17$heredoc_null_string =<<<EOT
18EOT;
19
20// initialising the string array
21
22$str_array = array(
23                    // string without any characters that can be backslashed
24                    'Hello world',
25
26                    // string with single quotes
27                    "how're you doing?",
28                    "don't disturb u'r neighbours",
29                    "don't disturb u'r neighbours''",
30                    '',
31                    '\'',
32                    "'",
33
34                    // string with double quotes
35                    'he said, "he will be on leave"',
36                    'he said, ""he will be on leave"',
37                    '"""PHP"""',
38                    "",
39                    "\"",
40                    '"',
41            "hello\"",
42
43                    // string with backslash characters
44                    'Is your name Ram\Krishna?',
45                    '\\0.0.0.0',
46                    'c:\php\testcase\addslashes',
47                    '\\',
48
49                    // string with nul characters
50                    'hello'.chr(0).'world',
51                    chr(0).'hello'.chr(0),
52                    chr(0).chr(0).'hello',
53                    chr(0),
54
55                    // mixed strings
56                    "'\\0.0.0.0'",
57                    "'\\0.0.0.0'".chr(0),
58                    chr(0)."'c:\php\'",
59                    '"\\0.0.0.0"',
60                    '"c:\php\"'.chr(0)."'",
61                    '"hello"'."'world'".chr(0).'//',
62
63            // string with hexadecimal number
64                    "0xABCDEF0123456789",
65                    "\x00",
66                    '!@#$%&*@$%#&/;:,<>',
67                    "hello\x00world",
68
69                    // heredoc strings
70                    $heredoc_string,
71                    $heredoc_null_string
72                  );
73
74$count = 1;
75// looping to test for all strings in $str_array
76foreach( $str_array as $str )  {
77  echo "\n-- Iteration $count --\n";
78  var_dump( addslashes($str) );
79  $count ++;
80}
81
82echo "Done\n";
83?>
84--EXPECTF--
85*** Testing addslashes() : with various strings containing characters to be backslashed ***
86
87-- Iteration 1 --
88string(11) "Hello world"
89
90-- Iteration 2 --
91string(18) "how\'re you doing?"
92
93-- Iteration 3 --
94string(30) "don\'t disturb u\'r neighbours"
95
96-- Iteration 4 --
97string(34) "don\'t disturb u\'r neighbours\'\'"
98
99-- Iteration 5 --
100string(0) ""
101
102-- Iteration 6 --
103string(2) "\'"
104
105-- Iteration 7 --
106string(2) "\'"
107
108-- Iteration 8 --
109string(32) "he said, \"he will be on leave\""
110
111-- Iteration 9 --
112string(34) "he said, \"\"he will be on leave\""
113
114-- Iteration 10 --
115string(15) "\"\"\"PHP\"\"\""
116
117-- Iteration 11 --
118string(0) ""
119
120-- Iteration 12 --
121string(2) "\""
122
123-- Iteration 13 --
124string(2) "\""
125
126-- Iteration 14 --
127string(7) "hello\""
128
129-- Iteration 15 --
130string(26) "Is your name Ram\\Krishna?"
131
132-- Iteration 16 --
133string(9) "\\0.0.0.0"
134
135-- Iteration 17 --
136string(29) "c:\\php\\testcase\\addslashes"
137
138-- Iteration 18 --
139string(2) "\\"
140
141-- Iteration 19 --
142string(12) "hello\0world"
143
144-- Iteration 20 --
145string(9) "\0hello\0"
146
147-- Iteration 21 --
148string(9) "\0\0hello"
149
150-- Iteration 22 --
151string(2) "\0"
152
153-- Iteration 23 --
154string(13) "\'\\0.0.0.0\'"
155
156-- Iteration 24 --
157string(15) "\'\\0.0.0.0\'\0"
158
159-- Iteration 25 --
160string(15) "\0\'c:\\php\\\'"
161
162-- Iteration 26 --
163string(13) "\"\\0.0.0.0\""
164
165-- Iteration 27 --
166string(17) "\"c:\\php\\\"\0\'"
167
168-- Iteration 28 --
169string(22) "\"hello\"\'world\'\0//"
170
171-- Iteration 29 --
172string(18) "0xABCDEF0123456789"
173
174-- Iteration 30 --
175string(2) "\0"
176
177-- Iteration 31 --
178string(18) "!@#$%&*@$%#&/;:,<>"
179
180-- Iteration 32 --
181string(12) "hello\0world"
182
183-- Iteration 33 --
184string(7%d) "This is line 1 of \'heredoc\' string
185This is line 2 of \"heredoc\" string"
186
187-- Iteration 34 --
188string(0) ""
189Done
190