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 11echo "*** Testing fnmatch() with file and various patterns ***\n"; 12$file_name = __DIR__."/match.tmp"; 13 14/* avoid using \, it breaks the pattern */ 15if (substr(PHP_OS, 0, 3) == 'WIN') { 16 $file_name = str_replace('\\','/', $file_name); 17} 18 19fopen($file_name, "w"); 20 21$pattern_arr = array( 220 => "*.tmp", 231 => "match*", 242 => "mat*", 253 => "mat*tmp", 264 => "m*t", 275 => "ma[pt]ch*", 286 => "*.t*", 297 => "***.tmp", 308 => "match**", 319 => "*.t*p", 3210 => "", 3311 => "match", 3412 => ".tmp", 3513 => "?match", 3614 => "match?tmp", 3715 => "?tmp", 3816 => "match?", 3917 => "?match?", 4018 => "match.tmp", 4119 => "/match.tmp", 4220 => "/match.tmp/", 4321 => 'match.tmp', 4422 => 'match.tmp\0', 4523 => "match.tmp\0", 4624 => "match\0.tmp", 4725 => chr(109).chr(97)."tch.tmp", 4826 => chr(109).chr(97).chr(116).chr(99).chr(104).".tmp", 4927 => chr(109).chr(97).chr(116).chr(99).chr(104).chr(46).chr(116).chr(120).chr(116), 5028 => chr(109).chr(97).chr(116).chr(99).chr(104).".".chr(116).chr(120).chr(116), 5129 => "MATCH.TMP", 5230 => "MATCH*", 5331 => $file_name, 54 55/* binary inputs */ 5632 => b"match*", 5733 => b"*.tmp", 5834 => b"mat*", 5935 => b"mat*tmp", 6036 => b"m*t", 61); 62 63for( $i = 0; $i<count($pattern_arr); $i++ ) { 64 echo "-- Iteration $i --\n"; 65 try { 66 var_dump( fnmatch($pattern_arr[$i], $file_name) ); 67 } catch (Error $e) { 68 echo $e->getMessage(), "\n"; 69 } 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 try { 82 var_dump( fnmatch($pattern[$i], $string[$j]) ); 83 } catch (Error $e) { 84 echo $e->getMessage(), "\n"; 85 } 86 } 87 } 88} 89 90echo "\n--- With Integers ---\n"; 91$int_arr = array( 92 16, 93 16.00, 94 020, 95 020.00, 96 0xF, 97 0xF0000 98); 99match_($int_arr, $int_arr); 100 101echo "\n--- With Strings ---\n"; 102$str_arr = array( 103 "string", 104 "string\0", 105 'string', 106 "str\0ing", 107 "stringstring", 108 109 /* binary input */ 110 b"string" 111); 112match_($str_arr, $str_arr); 113 114echo "\n--- With booleans ---\n"; 115$bool_arr = array( 116 TRUE, 117 true, 118 1, 119 10, 120 FALSE, 121 false, 122 0, 123 "", 124 "string" 125); 126match_($bool_arr, $bool_arr); 127 128echo "\n--- With NULL ---\n"; 129$null_arr = array( 130 NULL, 131 null, 132 "", 133 "\0", 134 "string", 135 0 136); 137match_($null_arr, $null_arr); 138 139echo "\n*** Done ***\n"; 140?> 141--EXPECT-- 142*** Testing fnmatch() with file and various patterns *** 143-- Iteration 0 -- 144bool(true) 145-- Iteration 1 -- 146bool(false) 147-- Iteration 2 -- 148bool(false) 149-- Iteration 3 -- 150bool(false) 151-- Iteration 4 -- 152bool(false) 153-- Iteration 5 -- 154bool(false) 155-- Iteration 6 -- 156bool(true) 157-- Iteration 7 -- 158bool(true) 159-- Iteration 8 -- 160bool(false) 161-- Iteration 9 -- 162bool(true) 163-- Iteration 10 -- 164bool(false) 165-- Iteration 11 -- 166bool(false) 167-- Iteration 12 -- 168bool(false) 169-- Iteration 13 -- 170bool(false) 171-- Iteration 14 -- 172bool(false) 173-- Iteration 15 -- 174bool(false) 175-- Iteration 16 -- 176bool(false) 177-- Iteration 17 -- 178bool(false) 179-- Iteration 18 -- 180bool(false) 181-- Iteration 19 -- 182bool(false) 183-- Iteration 20 -- 184bool(false) 185-- Iteration 21 -- 186bool(false) 187-- Iteration 22 -- 188bool(false) 189-- Iteration 23 -- 190fnmatch(): Argument #1 ($pattern) must not contain any null bytes 191-- Iteration 24 -- 192fnmatch(): Argument #1 ($pattern) must not contain any null bytes 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) 266fnmatch(): Argument #2 ($filename) must not contain any null bytes 267bool(true) 268fnmatch(): Argument #2 ($filename) must not contain any null bytes 269bool(false) 270bool(true) 271-- Iteration 1 -- 272fnmatch(): Argument #1 ($pattern) must not contain any null bytes 273fnmatch(): Argument #1 ($pattern) must not contain any null bytes 274fnmatch(): Argument #1 ($pattern) must not contain any null bytes 275fnmatch(): Argument #1 ($pattern) must not contain any null bytes 276fnmatch(): Argument #1 ($pattern) must not contain any null bytes 277fnmatch(): Argument #1 ($pattern) must not contain any null bytes 278-- Iteration 2 -- 279bool(true) 280fnmatch(): Argument #2 ($filename) must not contain any null bytes 281bool(true) 282fnmatch(): Argument #2 ($filename) must not contain any null bytes 283bool(false) 284bool(true) 285-- Iteration 3 -- 286fnmatch(): Argument #1 ($pattern) must not contain any null bytes 287fnmatch(): Argument #1 ($pattern) must not contain any null bytes 288fnmatch(): Argument #1 ($pattern) must not contain any null bytes 289fnmatch(): Argument #1 ($pattern) must not contain any null bytes 290fnmatch(): Argument #1 ($pattern) must not contain any null bytes 291fnmatch(): Argument #1 ($pattern) must not contain any null bytes 292-- Iteration 4 -- 293bool(false) 294fnmatch(): Argument #2 ($filename) must not contain any null bytes 295bool(false) 296fnmatch(): Argument #2 ($filename) must not contain any null bytes 297bool(true) 298bool(false) 299-- Iteration 5 -- 300bool(true) 301fnmatch(): Argument #2 ($filename) must not contain any null bytes 302bool(true) 303fnmatch(): Argument #2 ($filename) must not contain any null bytes 304bool(false) 305bool(true) 306 307--- With booleans --- 308-- Iteration 0 -- 309bool(true) 310bool(true) 311bool(true) 312bool(false) 313bool(false) 314bool(false) 315bool(false) 316bool(false) 317bool(false) 318-- Iteration 1 -- 319bool(true) 320bool(true) 321bool(true) 322bool(false) 323bool(false) 324bool(false) 325bool(false) 326bool(false) 327bool(false) 328-- Iteration 2 -- 329bool(true) 330bool(true) 331bool(true) 332bool(false) 333bool(false) 334bool(false) 335bool(false) 336bool(false) 337bool(false) 338-- Iteration 3 -- 339bool(false) 340bool(false) 341bool(false) 342bool(true) 343bool(false) 344bool(false) 345bool(false) 346bool(false) 347bool(false) 348-- Iteration 4 -- 349bool(false) 350bool(false) 351bool(false) 352bool(false) 353bool(true) 354bool(true) 355bool(false) 356bool(true) 357bool(false) 358-- Iteration 5 -- 359bool(false) 360bool(false) 361bool(false) 362bool(false) 363bool(true) 364bool(true) 365bool(false) 366bool(true) 367bool(false) 368-- Iteration 6 -- 369bool(false) 370bool(false) 371bool(false) 372bool(false) 373bool(false) 374bool(false) 375bool(true) 376bool(false) 377bool(false) 378-- Iteration 7 -- 379bool(false) 380bool(false) 381bool(false) 382bool(false) 383bool(true) 384bool(true) 385bool(false) 386bool(true) 387bool(false) 388-- Iteration 8 -- 389bool(false) 390bool(false) 391bool(false) 392bool(false) 393bool(false) 394bool(false) 395bool(false) 396bool(false) 397bool(true) 398 399--- With NULL --- 400-- Iteration 0 -- 401bool(true) 402bool(true) 403bool(true) 404fnmatch(): Argument #2 ($filename) must not contain any null bytes 405bool(false) 406bool(false) 407-- Iteration 1 -- 408bool(true) 409bool(true) 410bool(true) 411fnmatch(): Argument #2 ($filename) must not contain any null bytes 412bool(false) 413bool(false) 414-- Iteration 2 -- 415bool(true) 416bool(true) 417bool(true) 418fnmatch(): Argument #2 ($filename) must not contain any null bytes 419bool(false) 420bool(false) 421-- Iteration 3 -- 422fnmatch(): Argument #1 ($pattern) must not contain any null bytes 423fnmatch(): Argument #1 ($pattern) must not contain any null bytes 424fnmatch(): Argument #1 ($pattern) must not contain any null bytes 425fnmatch(): Argument #1 ($pattern) must not contain any null bytes 426fnmatch(): Argument #1 ($pattern) must not contain any null bytes 427fnmatch(): Argument #1 ($pattern) must not contain any null bytes 428-- Iteration 4 -- 429bool(false) 430bool(false) 431bool(false) 432fnmatch(): Argument #2 ($filename) must not contain any null bytes 433bool(true) 434bool(false) 435-- Iteration 5 -- 436bool(false) 437bool(false) 438bool(false) 439fnmatch(): Argument #2 ($filename) must not contain any null bytes 440bool(false) 441bool(true) 442 443*** Done *** 444