1--TEST--
2Test htmlspecialchars() function
3--FILE--
4<?php
5/* Prototype: string htmlspecialchars ( string $string [, int $quote_style [, string $charset]] );
6   Description: Convert special characters to HTML entities
7*/
8
9/* retrieving htmlspecialchars from the ANSI character table */
10echo "*** Retrieving htmlspecialchars for 256 characters ***\n";
11for($i=0; $i<256; $i++)
12var_dump( bin2hex( htmlspecialchars("chr($i)") ) );
13
14/* giving NULL as the argument */
15echo "\n*** Testing htmlspecialchars() with NULL as first, second and third argument ***\n";
16var_dump( htmlspecialchars("<br>", NULL, 'iso-8859-1') );
17var_dump( htmlspecialchars("<br>", ENT_NOQUOTES, NULL) );
18var_dump( htmlspecialchars("<br>", ENT_QUOTES, NULL) );
19var_dump( htmlspecialchars("<br>", ENT_COMPAT, NULL) );
20var_dump( htmlspecialchars(NULL, NULL, NULL) );
21
22/* giving long string to check for proper memory re-allocation */
23echo "\n*** Checking a long string for proper memory allocation ***\n";
24var_dump( htmlspecialchars("<br>Testing<p>New file.</p><p><br>File <b><i><u>WORKS!!!</i></u></b></p><br><p>End of file!!!</p>", ENT_QUOTES, 'iso-8859-1' ) );
25
26/* Giving a normal string */
27echo "\n*** Testing a normal string with htmlspecialchars() ***\n";
28var_dump( htmlspecialchars("<br>Testing<p>New file.</p> ", ENT_QUOTES, 'iso-8859-1' ) );
29
30/* checking behavior of quote */
31echo "\n*** Testing htmlspecialchars() on a quote...\n";
32$str = "A 'quote' is <b>bold</b>";
33var_dump( htmlspecialchars($str) );
34var_dump( htmlspecialchars($str, ENT_QUOTES) );
35var_dump( htmlspecialchars($str, ENT_NOQUOTES) );
36var_dump( htmlspecialchars($str, ENT_COMPAT) );
37
38echo "\n*** Testing error conditions ***\n";
39/* zero argument */
40var_dump( htmlspecialchars() );
41
42/* giving arguments more than expected */
43var_dump( htmlspecialchars("<br>",ENT_QUOTES,'iso-8859-1', "test2") );
44
45echo "Done\n"
46?>
47--EXPECTF--
48*** Retrieving htmlspecialchars for 256 characters ***
49string(12) "636872283029"
50string(12) "636872283129"
51string(12) "636872283229"
52string(12) "636872283329"
53string(12) "636872283429"
54string(12) "636872283529"
55string(12) "636872283629"
56string(12) "636872283729"
57string(12) "636872283829"
58string(12) "636872283929"
59string(14) "63687228313029"
60string(14) "63687228313129"
61string(14) "63687228313229"
62string(14) "63687228313329"
63string(14) "63687228313429"
64string(14) "63687228313529"
65string(14) "63687228313629"
66string(14) "63687228313729"
67string(14) "63687228313829"
68string(14) "63687228313929"
69string(14) "63687228323029"
70string(14) "63687228323129"
71string(14) "63687228323229"
72string(14) "63687228323329"
73string(14) "63687228323429"
74string(14) "63687228323529"
75string(14) "63687228323629"
76string(14) "63687228323729"
77string(14) "63687228323829"
78string(14) "63687228323929"
79string(14) "63687228333029"
80string(14) "63687228333129"
81string(14) "63687228333229"
82string(14) "63687228333329"
83string(14) "63687228333429"
84string(14) "63687228333529"
85string(14) "63687228333629"
86string(14) "63687228333729"
87string(14) "63687228333829"
88string(14) "63687228333929"
89string(14) "63687228343029"
90string(14) "63687228343129"
91string(14) "63687228343229"
92string(14) "63687228343329"
93string(14) "63687228343429"
94string(14) "63687228343529"
95string(14) "63687228343629"
96string(14) "63687228343729"
97string(14) "63687228343829"
98string(14) "63687228343929"
99string(14) "63687228353029"
100string(14) "63687228353129"
101string(14) "63687228353229"
102string(14) "63687228353329"
103string(14) "63687228353429"
104string(14) "63687228353529"
105string(14) "63687228353629"
106string(14) "63687228353729"
107string(14) "63687228353829"
108string(14) "63687228353929"
109string(14) "63687228363029"
110string(14) "63687228363129"
111string(14) "63687228363229"
112string(14) "63687228363329"
113string(14) "63687228363429"
114string(14) "63687228363529"
115string(14) "63687228363629"
116string(14) "63687228363729"
117string(14) "63687228363829"
118string(14) "63687228363929"
119string(14) "63687228373029"
120string(14) "63687228373129"
121string(14) "63687228373229"
122string(14) "63687228373329"
123string(14) "63687228373429"
124string(14) "63687228373529"
125string(14) "63687228373629"
126string(14) "63687228373729"
127string(14) "63687228373829"
128string(14) "63687228373929"
129string(14) "63687228383029"
130string(14) "63687228383129"
131string(14) "63687228383229"
132string(14) "63687228383329"
133string(14) "63687228383429"
134string(14) "63687228383529"
135string(14) "63687228383629"
136string(14) "63687228383729"
137string(14) "63687228383829"
138string(14) "63687228383929"
139string(14) "63687228393029"
140string(14) "63687228393129"
141string(14) "63687228393229"
142string(14) "63687228393329"
143string(14) "63687228393429"
144string(14) "63687228393529"
145string(14) "63687228393629"
146string(14) "63687228393729"
147string(14) "63687228393829"
148string(14) "63687228393929"
149string(16) "6368722831303029"
150string(16) "6368722831303129"
151string(16) "6368722831303229"
152string(16) "6368722831303329"
153string(16) "6368722831303429"
154string(16) "6368722831303529"
155string(16) "6368722831303629"
156string(16) "6368722831303729"
157string(16) "6368722831303829"
158string(16) "6368722831303929"
159string(16) "6368722831313029"
160string(16) "6368722831313129"
161string(16) "6368722831313229"
162string(16) "6368722831313329"
163string(16) "6368722831313429"
164string(16) "6368722831313529"
165string(16) "6368722831313629"
166string(16) "6368722831313729"
167string(16) "6368722831313829"
168string(16) "6368722831313929"
169string(16) "6368722831323029"
170string(16) "6368722831323129"
171string(16) "6368722831323229"
172string(16) "6368722831323329"
173string(16) "6368722831323429"
174string(16) "6368722831323529"
175string(16) "6368722831323629"
176string(16) "6368722831323729"
177string(16) "6368722831323829"
178string(16) "6368722831323929"
179string(16) "6368722831333029"
180string(16) "6368722831333129"
181string(16) "6368722831333229"
182string(16) "6368722831333329"
183string(16) "6368722831333429"
184string(16) "6368722831333529"
185string(16) "6368722831333629"
186string(16) "6368722831333729"
187string(16) "6368722831333829"
188string(16) "6368722831333929"
189string(16) "6368722831343029"
190string(16) "6368722831343129"
191string(16) "6368722831343229"
192string(16) "6368722831343329"
193string(16) "6368722831343429"
194string(16) "6368722831343529"
195string(16) "6368722831343629"
196string(16) "6368722831343729"
197string(16) "6368722831343829"
198string(16) "6368722831343929"
199string(16) "6368722831353029"
200string(16) "6368722831353129"
201string(16) "6368722831353229"
202string(16) "6368722831353329"
203string(16) "6368722831353429"
204string(16) "6368722831353529"
205string(16) "6368722831353629"
206string(16) "6368722831353729"
207string(16) "6368722831353829"
208string(16) "6368722831353929"
209string(16) "6368722831363029"
210string(16) "6368722831363129"
211string(16) "6368722831363229"
212string(16) "6368722831363329"
213string(16) "6368722831363429"
214string(16) "6368722831363529"
215string(16) "6368722831363629"
216string(16) "6368722831363729"
217string(16) "6368722831363829"
218string(16) "6368722831363929"
219string(16) "6368722831373029"
220string(16) "6368722831373129"
221string(16) "6368722831373229"
222string(16) "6368722831373329"
223string(16) "6368722831373429"
224string(16) "6368722831373529"
225string(16) "6368722831373629"
226string(16) "6368722831373729"
227string(16) "6368722831373829"
228string(16) "6368722831373929"
229string(16) "6368722831383029"
230string(16) "6368722831383129"
231string(16) "6368722831383229"
232string(16) "6368722831383329"
233string(16) "6368722831383429"
234string(16) "6368722831383529"
235string(16) "6368722831383629"
236string(16) "6368722831383729"
237string(16) "6368722831383829"
238string(16) "6368722831383929"
239string(16) "6368722831393029"
240string(16) "6368722831393129"
241string(16) "6368722831393229"
242string(16) "6368722831393329"
243string(16) "6368722831393429"
244string(16) "6368722831393529"
245string(16) "6368722831393629"
246string(16) "6368722831393729"
247string(16) "6368722831393829"
248string(16) "6368722831393929"
249string(16) "6368722832303029"
250string(16) "6368722832303129"
251string(16) "6368722832303229"
252string(16) "6368722832303329"
253string(16) "6368722832303429"
254string(16) "6368722832303529"
255string(16) "6368722832303629"
256string(16) "6368722832303729"
257string(16) "6368722832303829"
258string(16) "6368722832303929"
259string(16) "6368722832313029"
260string(16) "6368722832313129"
261string(16) "6368722832313229"
262string(16) "6368722832313329"
263string(16) "6368722832313429"
264string(16) "6368722832313529"
265string(16) "6368722832313629"
266string(16) "6368722832313729"
267string(16) "6368722832313829"
268string(16) "6368722832313929"
269string(16) "6368722832323029"
270string(16) "6368722832323129"
271string(16) "6368722832323229"
272string(16) "6368722832323329"
273string(16) "6368722832323429"
274string(16) "6368722832323529"
275string(16) "6368722832323629"
276string(16) "6368722832323729"
277string(16) "6368722832323829"
278string(16) "6368722832323929"
279string(16) "6368722832333029"
280string(16) "6368722832333129"
281string(16) "6368722832333229"
282string(16) "6368722832333329"
283string(16) "6368722832333429"
284string(16) "6368722832333529"
285string(16) "6368722832333629"
286string(16) "6368722832333729"
287string(16) "6368722832333829"
288string(16) "6368722832333929"
289string(16) "6368722832343029"
290string(16) "6368722832343129"
291string(16) "6368722832343229"
292string(16) "6368722832343329"
293string(16) "6368722832343429"
294string(16) "6368722832343529"
295string(16) "6368722832343629"
296string(16) "6368722832343729"
297string(16) "6368722832343829"
298string(16) "6368722832343929"
299string(16) "6368722832353029"
300string(16) "6368722832353129"
301string(16) "6368722832353229"
302string(16) "6368722832353329"
303string(16) "6368722832353429"
304string(16) "6368722832353529"
305
306*** Testing htmlspecialchars() with NULL as first, second and third argument ***
307string(10) "&lt;br&gt;"
308string(10) "&lt;br&gt;"
309string(10) "&lt;br&gt;"
310string(10) "&lt;br&gt;"
311string(0) ""
312
313*** Checking a long string for proper memory allocation ***
314string(187) "&lt;br&gt;Testing&lt;p&gt;New file.&lt;/p&gt;&lt;p&gt;&lt;br&gt;File &lt;b&gt;&lt;i&gt;&lt;u&gt;WORKS!!!&lt;/i&gt;&lt;/u&gt;&lt;/b&gt;&lt;/p&gt;&lt;br&gt;&lt;p&gt;End of file!!!&lt;/p&gt;"
315
316*** Testing a normal string with htmlspecialchars() ***
317string(46) "&lt;br&gt;Testing&lt;p&gt;New file.&lt;/p&gt; "
318
319*** Testing htmlspecialchars() on a quote...
320string(36) "A 'quote' is &lt;b&gt;bold&lt;/b&gt;"
321string(46) "A &#039;quote&#039; is &lt;b&gt;bold&lt;/b&gt;"
322string(36) "A 'quote' is &lt;b&gt;bold&lt;/b&gt;"
323string(36) "A 'quote' is &lt;b&gt;bold&lt;/b&gt;"
324
325*** Testing error conditions ***
326
327Warning: htmlspecialchars() expects at least 1 parameter, 0 given in %s on line %d
328NULL
329string(10) "&lt;br&gt;"
330Done
331