1--TEST--
2Test fnmatch() function: Variations
3--SKIPIF--
4<?php
5if (!function_exists('fnmatch'))
6    die("skip fnmatch() function is not available");
7?>
8--FILE--
9<?php
10/* Prototype: bool fnmatch ( string $pattern, string $string [, int $flags] )
11   Description: fnmatch() checks if the passed string would match
12     the given shell wildcard pattern.
13*/
14
15echo "*** Testing fnmatch() with file and various patterns ***\n";
16$file_name = dirname(__FILE__)."/match.tmp";
17
18/* avoid using \, it breaks the pattern */
19if (substr(PHP_OS, 0, 3) == 'WIN') {
20    $file_name = str_replace('\\','/', $file_name);
21}
22
23fopen($file_name, "w");
24
25$pattern_arr = array(
260 => "*.tmp",
271 => "match*",
282 => "mat*",
293 => "mat*tmp",
304 => "m*t",
315 => "ma[pt]ch*",
326 => "*.t*",
337 => "***.tmp",
348 => "match**",
359 => "*.t*p",
3610 => "",
3711 => "match",
3812 => ".tmp",
3913 => "?match",
4014 => "match?tmp",
4115 => "?tmp",
4216 => "match?",
4317 => "?match?",
4418 => "match.tmp",
4519 => "/match.tmp",
4620 => "/match.tmp/",
4721 => 'match.tmp',
4822 => 'match.tmp\0',
4923 => "match.tmp\0",
5024 => "match\0.tmp",
5125 => chr(109).chr(97)."tch.tmp",
5226 => chr(109).chr(97).chr(116).chr(99).chr(104).".tmp",
5327 => chr(109).chr(97).chr(116).chr(99).chr(104).chr(46).chr(116).chr(120).chr(116),
5428 => chr(109).chr(97).chr(116).chr(99).chr(104).".".chr(116).chr(120).chr(116),
5529 => "MATCH.TMP",
5630 => "MATCH*",
5731 => $file_name,
58
59/* binary inputs */
6032 => b"match*",
6133 => b"*.tmp",
6234 => b"mat*",
6335 => b"mat*tmp",
6436 => b"m*t",
65);
66
67for( $i = 0; $i<count($pattern_arr); $i++ ) {
68  echo "-- Iteration $i --\n";
69  var_dump( fnmatch($pattern_arr[$i], $file_name) );
70}
71unlink($file_name);
72
73
74echo "\n*** Testing fnmatch() with other types other than files ***";
75
76/* defining a common function */
77function match( $pattern, $string ) {
78  for( $i = 0; $i<count($pattern); $i++ ) {
79    echo "-- Iteration $i --\n";
80    for( $j = 0; $j<count($string); $j++ ) {
81    var_dump( fnmatch($pattern[$i], $string[$j]) );
82    }
83  }
84}
85
86echo "\n--- With Integers ---\n";
87$int_arr = array(
88  16,
89  16.00,
90  020,
91  020.00,
92  0xF,
93  0xF0000
94);
95match($int_arr, $int_arr);
96
97echo "\n--- With Strings ---\n";
98$str_arr = array(
99  "string",
100  "string\0",
101  'string',
102  "str\0ing",
103  "stringstring",
104
105  /* binary input */
106  b"string"
107);
108match($str_arr, $str_arr);
109
110echo "\n--- With booleans ---\n";
111$bool_arr = array(
112  TRUE,
113  true,
114  1,
115  10,
116  FALSE,
117  false,
118  0,
119  "",
120  "string"
121);
122match($bool_arr, $bool_arr);
123
124echo "\n--- With NULL ---\n";
125$null_arr = array(
126  NULL,
127  null,
128  "",
129  "\0",
130  "string",
131  0
132);
133match($null_arr, $null_arr);
134
135echo "\n*** Done ***\n";
136?>
137--EXPECTF--
138*** Testing fnmatch() with file and various patterns ***
139-- Iteration 0 --
140bool(true)
141-- Iteration 1 --
142bool(false)
143-- Iteration 2 --
144bool(false)
145-- Iteration 3 --
146bool(false)
147-- Iteration 4 --
148bool(false)
149-- Iteration 5 --
150bool(false)
151-- Iteration 6 --
152bool(true)
153-- Iteration 7 --
154bool(true)
155-- Iteration 8 --
156bool(false)
157-- Iteration 9 --
158bool(true)
159-- Iteration 10 --
160bool(false)
161-- Iteration 11 --
162bool(false)
163-- Iteration 12 --
164bool(false)
165-- Iteration 13 --
166bool(false)
167-- Iteration 14 --
168bool(false)
169-- Iteration 15 --
170bool(false)
171-- Iteration 16 --
172bool(false)
173-- Iteration 17 --
174bool(false)
175-- Iteration 18 --
176bool(false)
177-- Iteration 19 --
178bool(false)
179-- Iteration 20 --
180bool(false)
181-- Iteration 21 --
182bool(false)
183-- Iteration 22 --
184bool(false)
185-- Iteration 23 --
186
187Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
188NULL
189-- Iteration 24 --
190
191Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
192NULL
193-- Iteration 25 --
194bool(false)
195-- Iteration 26 --
196bool(false)
197-- Iteration 27 --
198bool(false)
199-- Iteration 28 --
200bool(false)
201-- Iteration 29 --
202bool(false)
203-- Iteration 30 --
204bool(false)
205-- Iteration 31 --
206bool(true)
207-- Iteration 32 --
208bool(false)
209-- Iteration 33 --
210bool(true)
211-- Iteration 34 --
212bool(false)
213-- Iteration 35 --
214bool(false)
215-- Iteration 36 --
216bool(false)
217
218*** Testing fnmatch() with other types other than files ***
219--- With Integers ---
220-- Iteration 0 --
221bool(true)
222bool(true)
223bool(true)
224bool(false)
225bool(false)
226bool(false)
227-- Iteration 1 --
228bool(true)
229bool(true)
230bool(true)
231bool(false)
232bool(false)
233bool(false)
234-- Iteration 2 --
235bool(true)
236bool(true)
237bool(true)
238bool(false)
239bool(false)
240bool(false)
241-- Iteration 3 --
242bool(false)
243bool(false)
244bool(false)
245bool(true)
246bool(false)
247bool(false)
248-- Iteration 4 --
249bool(false)
250bool(false)
251bool(false)
252bool(false)
253bool(true)
254bool(false)
255-- Iteration 5 --
256bool(false)
257bool(false)
258bool(false)
259bool(false)
260bool(false)
261bool(true)
262
263--- With Strings ---
264-- Iteration 0 --
265bool(true)
266
267Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
268NULL
269bool(true)
270
271Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
272NULL
273bool(false)
274bool(true)
275-- Iteration 1 --
276
277Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
278NULL
279
280Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
281NULL
282
283Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
284NULL
285
286Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
287NULL
288
289Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
290NULL
291
292Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
293NULL
294-- Iteration 2 --
295bool(true)
296
297Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
298NULL
299bool(true)
300
301Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
302NULL
303bool(false)
304bool(true)
305-- Iteration 3 --
306
307Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
308NULL
309
310Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
311NULL
312
313Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
314NULL
315
316Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
317NULL
318
319Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
320NULL
321
322Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
323NULL
324-- Iteration 4 --
325bool(false)
326
327Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
328NULL
329bool(false)
330
331Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
332NULL
333bool(true)
334bool(false)
335-- Iteration 5 --
336bool(true)
337
338Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
339NULL
340bool(true)
341
342Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
343NULL
344bool(false)
345bool(true)
346
347--- With booleans ---
348-- Iteration 0 --
349bool(true)
350bool(true)
351bool(true)
352bool(false)
353bool(false)
354bool(false)
355bool(false)
356bool(false)
357bool(false)
358-- Iteration 1 --
359bool(true)
360bool(true)
361bool(true)
362bool(false)
363bool(false)
364bool(false)
365bool(false)
366bool(false)
367bool(false)
368-- Iteration 2 --
369bool(true)
370bool(true)
371bool(true)
372bool(false)
373bool(false)
374bool(false)
375bool(false)
376bool(false)
377bool(false)
378-- Iteration 3 --
379bool(false)
380bool(false)
381bool(false)
382bool(true)
383bool(false)
384bool(false)
385bool(false)
386bool(false)
387bool(false)
388-- Iteration 4 --
389bool(false)
390bool(false)
391bool(false)
392bool(false)
393bool(true)
394bool(true)
395bool(false)
396bool(true)
397bool(false)
398-- Iteration 5 --
399bool(false)
400bool(false)
401bool(false)
402bool(false)
403bool(true)
404bool(true)
405bool(false)
406bool(true)
407bool(false)
408-- Iteration 6 --
409bool(false)
410bool(false)
411bool(false)
412bool(false)
413bool(false)
414bool(false)
415bool(true)
416bool(false)
417bool(false)
418-- Iteration 7 --
419bool(false)
420bool(false)
421bool(false)
422bool(false)
423bool(true)
424bool(true)
425bool(false)
426bool(true)
427bool(false)
428-- Iteration 8 --
429bool(false)
430bool(false)
431bool(false)
432bool(false)
433bool(false)
434bool(false)
435bool(false)
436bool(false)
437bool(true)
438
439--- With NULL ---
440-- Iteration 0 --
441bool(true)
442bool(true)
443bool(true)
444
445Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
446NULL
447bool(false)
448bool(false)
449-- Iteration 1 --
450bool(true)
451bool(true)
452bool(true)
453
454Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
455NULL
456bool(false)
457bool(false)
458-- Iteration 2 --
459bool(true)
460bool(true)
461bool(true)
462
463Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
464NULL
465bool(false)
466bool(false)
467-- Iteration 3 --
468
469Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
470NULL
471
472Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
473NULL
474
475Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
476NULL
477
478Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
479NULL
480
481Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
482NULL
483
484Warning: fnmatch() expects parameter 1 to be a valid path, string given in %s on line %d
485NULL
486-- Iteration 4 --
487bool(false)
488bool(false)
489bool(false)
490
491Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
492NULL
493bool(true)
494bool(false)
495-- Iteration 5 --
496bool(false)
497bool(false)
498bool(false)
499
500Warning: fnmatch() expects parameter 2 to be a valid path, string given in %s on line %d
501NULL
502bool(false)
503bool(true)
504
505*** Done ***
506