1--TEST-- 2Test basename() function : usage variations 3--FILE-- 4<?php 5/* Prototype: string basename ( string $path [, string $suffix] ); 6 Description: Given a string containing a path to a file, 7 this function will return the base name of the file. 8 If the filename ends in suffix this will also be cut off. 9*/ 10$file_path_variations = array ( 11 /* paths with shortcut home dir char, with suffix variation */ 12 array("~/home/user/bar"), 13 array("~/home/user/bar", ""), 14 array("~/home/user/bar", NULL), 15 array("~/home/user/bar", ' '), 16 array("~/home/user/bar.tar", ".tar"), 17 array("~/home/user/bar.tar", "~"), 18 array("~/home/user/bar.tar/", "~"), 19 array("~/home/user/bar.tar/", ""), 20 array("~/home/user/bar.tar", NULL), 21 array("~/home/user/bar.tar", ''), 22 array("~/home/user/bar.tar", " "), 23 24 /* paths with hostname:dir notation, with suffix variation */ 25 array("hostname:/home/usr/bar.tar"), 26 array("hostname:/home/user/bar.tar", "home"), 27 array("hostname:/home/user/tbar.gz", "bar.gz"), 28 array("hostname:/home/user/tbar.gz", "/bar.gz"), 29 array("hostname:/home/user/tbar.gz", "/bar.gz/"), 30 array("hostname:/home/user/tbar.gz/", "/bar.gz/"), 31 array("hostname:/home/user/tbar.gz/", "/bar.gz/"), 32 array("hostname:/home/user/My Pics.gz/", "/bar.gz/"), 33 array("hostname:/home/user/My Pics.gz/", "Pics.gz/"), 34 array("hostname:/home/user/My Pics.gz/", "Pics.gz"), 35 array("hostname:/home/user/My Pics.gz/", ".gz"), 36 array("hostname:/home/user/My Pics.gz/"), 37 array("hostname:/home/user/My Pics.gz/", NULL), 38 array("hostname:/home/user/My Pics.gz/", ' '), 39 array("hostname:/home/user/My Pics.gz/", ''), 40 array("hostname:/home/user/My Pics.gz/", "My Pics.gz"), 41 42 /* paths with numeirc strings */ 43 array("10.5"), 44 array("10.5", ".5"), 45 array("10.5", "10.5"), 46 array("10"), 47 array("105", "5"), 48 array("/10.5"), 49 array("10.5/"), 50 array("10/10.gz"), 51 array("0"), 52 array('0'), 53 54 /* paths and suffix given as same */ 55 array("bar.gz", "bar.gz"), 56 array("/bar.gz", "/bar.gz"), 57 array("/bar.gz/", "/bar.gz/"), 58 array(" ", " "), 59 array(' ', ' '), 60 array(NULL, NULL), 61 62 /* path with spaces */ 63 array(" "), 64 array(' '), 65 66 /* empty paths */ 67 array(""), 68 array(''), 69 array(NULL) 70); 71 72function check_basename( $path_arrays ) { 73 $loop_counter = 1; 74 foreach ($path_arrays as $path) { 75 echo "\n--Iteration $loop_counter--\n"; $loop_counter++; 76 if( 1 == count($path) ) { // no suffix provided 77 var_dump( basename($path[0]) ); 78 } else { // path as well as suffix provided, 79 var_dump( basename($path[0], $path[1]) ); 80 } 81 } 82} 83 84echo "*** Testing possible variations in path and suffix ***\n"; 85check_basename( $file_path_variations ); 86 87echo "Done\n"; 88 89--EXPECTF-- 90*** Testing possible variations in path and suffix *** 91 92--Iteration 1-- 93string(3) "bar" 94 95--Iteration 2-- 96string(3) "bar" 97 98--Iteration 3-- 99string(3) "bar" 100 101--Iteration 4-- 102string(3) "bar" 103 104--Iteration 5-- 105string(3) "bar" 106 107--Iteration 6-- 108string(7) "bar.tar" 109 110--Iteration 7-- 111string(7) "bar.tar" 112 113--Iteration 8-- 114string(7) "bar.tar" 115 116--Iteration 9-- 117string(7) "bar.tar" 118 119--Iteration 10-- 120string(7) "bar.tar" 121 122--Iteration 11-- 123string(7) "bar.tar" 124 125--Iteration 12-- 126string(7) "bar.tar" 127 128--Iteration 13-- 129string(7) "bar.tar" 130 131--Iteration 14-- 132string(1) "t" 133 134--Iteration 15-- 135string(7) "tbar.gz" 136 137--Iteration 16-- 138string(7) "tbar.gz" 139 140--Iteration 17-- 141string(7) "tbar.gz" 142 143--Iteration 18-- 144string(7) "tbar.gz" 145 146--Iteration 19-- 147string(10) "My Pics.gz" 148 149--Iteration 20-- 150string(10) "My Pics.gz" 151 152--Iteration 21-- 153string(3) "My " 154 155--Iteration 22-- 156string(7) "My Pics" 157 158--Iteration 23-- 159string(10) "My Pics.gz" 160 161--Iteration 24-- 162string(10) "My Pics.gz" 163 164--Iteration 25-- 165string(10) "My Pics.gz" 166 167--Iteration 26-- 168string(10) "My Pics.gz" 169 170--Iteration 27-- 171string(10) "My Pics.gz" 172 173--Iteration 28-- 174string(4) "10.5" 175 176--Iteration 29-- 177string(2) "10" 178 179--Iteration 30-- 180string(4) "10.5" 181 182--Iteration 31-- 183string(2) "10" 184 185--Iteration 32-- 186string(2) "10" 187 188--Iteration 33-- 189string(4) "10.5" 190 191--Iteration 34-- 192string(4) "10.5" 193 194--Iteration 35-- 195string(5) "10.gz" 196 197--Iteration 36-- 198string(1) "0" 199 200--Iteration 37-- 201string(1) "0" 202 203--Iteration 38-- 204string(6) "bar.gz" 205 206--Iteration 39-- 207string(6) "bar.gz" 208 209--Iteration 40-- 210string(6) "bar.gz" 211 212--Iteration 41-- 213string(1) " " 214 215--Iteration 42-- 216string(1) " " 217 218--Iteration 43-- 219string(0) "" 220 221--Iteration 44-- 222string(1) " " 223 224--Iteration 45-- 225string(1) " " 226 227--Iteration 46-- 228string(0) "" 229 230--Iteration 47-- 231string(0) "" 232 233--Iteration 48-- 234string(0) "" 235Done 236