1--TEST-- 2Test basename() function : basic functionality 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_paths = array ( 11 /* simple paths */ 12 array("bar"), 13 array("/foo/bar"), 14 array("foo/bar"), 15 array("/bar"), 16 17 /* simple paths with trailing slashes */ 18 array("bar/"), 19 array("/bar/"), 20 array("/foo/bar/"), 21 array("foo/bar/"), 22 array("/bar/"), 23 24 /* paths with suffix removal */ 25 array("bar.gz", ".gz"), 26 array("bar.gz", "bar.gz"), 27 array("/foo/bar.gz", ".gz"), 28 array("foo/bar.gz", ".gz"), 29 array("/bar.gz", ".gz"), 30 31 /* paths with suffix and trailing slashes with suffix removal*/ 32 array("bar.gz/", ".gz"), 33 array("/bar.gz/", ".gz"), 34 array("/foo/bar.gz/", ".gz"), 35 array("foo/bar.gz/", ".gz"), 36 array("/bar.gz/", ".gz"), 37 38 /* paths with basename only suffix, with suffix removal*/ 39 array("/.gz", ".gz"), 40 array(".gz", ".gz"), 41 array("/foo/.gz", ".gz"), 42 43 /* paths with basename only suffix & trailing slashes, with suffix removal*/ 44 array(".gz/", ".gz"), 45 array("/foo/.gz/", ".gz"), 46 array("foo/.gz/", ".gz"), 47 48 /* paths with binary value to check if the function is binary safe*/ 49 array("foo".chr(0)."bar"), 50 array("/foo".chr(0)."bar"), 51 array("/foo".chr(0)."bar/"), 52 array("foo".chr(0)."bar/"), 53 array("foo".chr(0)."bar/test"), 54 array("/foo".chr(0)."bar/bar.gz", ".gz"), 55 array("/foo".chr(0)."bar/bar.gz") 56); 57 58function check_basename( $path_arrays ) { 59 $loop_counter = 1; 60 foreach ($path_arrays as $path) { 61 echo "\n--Iteration $loop_counter--\n"; $loop_counter++; 62 if( 1 == count($path) ) { // no suffix provided 63 var_dump( basename($path[0]) ); 64 } else { // path as well as suffix provided, 65 var_dump( basename($path[0], $path[1]) ); 66 } 67 } 68} 69 70echo "*** Testing basic operations ***\n"; 71check_basename( $file_paths ); 72 73echo "Done\n"; 74?> 75--EXPECT-- 76*** Testing basic operations *** 77 78--Iteration 1-- 79string(3) "bar" 80 81--Iteration 2-- 82string(3) "bar" 83 84--Iteration 3-- 85string(3) "bar" 86 87--Iteration 4-- 88string(3) "bar" 89 90--Iteration 5-- 91string(3) "bar" 92 93--Iteration 6-- 94string(3) "bar" 95 96--Iteration 7-- 97string(3) "bar" 98 99--Iteration 8-- 100string(3) "bar" 101 102--Iteration 9-- 103string(3) "bar" 104 105--Iteration 10-- 106string(3) "bar" 107 108--Iteration 11-- 109string(6) "bar.gz" 110 111--Iteration 12-- 112string(3) "bar" 113 114--Iteration 13-- 115string(3) "bar" 116 117--Iteration 14-- 118string(3) "bar" 119 120--Iteration 15-- 121string(3) "bar" 122 123--Iteration 16-- 124string(3) "bar" 125 126--Iteration 17-- 127string(3) "bar" 128 129--Iteration 18-- 130string(3) "bar" 131 132--Iteration 19-- 133string(3) "bar" 134 135--Iteration 20-- 136string(3) ".gz" 137 138--Iteration 21-- 139string(3) ".gz" 140 141--Iteration 22-- 142string(3) ".gz" 143 144--Iteration 23-- 145string(3) ".gz" 146 147--Iteration 24-- 148string(3) ".gz" 149 150--Iteration 25-- 151string(3) ".gz" 152 153--Iteration 26-- 154string(7) "foobar" 155 156--Iteration 27-- 157string(7) "foobar" 158 159--Iteration 28-- 160string(7) "foobar" 161 162--Iteration 29-- 163string(7) "foobar" 164 165--Iteration 30-- 166string(4) "test" 167 168--Iteration 31-- 169string(3) "bar" 170 171--Iteration 32-- 172string(6) "bar.gz" 173Done 174