fnmatch_variation.phpt 9.2 KB

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