1--TEST-- 2Testing substr() function 3--FILE-- 4<?php 5 6$strings_array = array( "", 12345, "abcdef", "123abc", "_123abc"); 7 8$counter = 1; 9foreach ($strings_array as $str) { 10 /* variations with two arguments */ 11 /* start values >, < and = 0 */ 12 13 echo ("\n--- Iteration ".$counter." ---\n"); 14 echo ("\n-- Variations for two arguments --\n"); 15 var_dump ( substr($str, 1) ); 16 var_dump ( substr($str, 0) ); 17 var_dump ( substr($str, -2) ); 18 19 /* variations with three arguments */ 20 /* start value variations with length values */ 21 22 echo ("\n-- Variations for three arguments --\n"); 23 var_dump ( substr($str, 1, 3) ); 24 var_dump ( substr($str, 1, 0) ); 25 var_dump ( substr($str, 1, -3) ); 26 var_dump ( substr($str, 0, 3) ); 27 var_dump ( substr($str, 0, 0) ); 28 var_dump ( substr($str, 0, -3) ); 29 var_dump ( substr($str, -2, 3) ); 30 var_dump ( substr($str, -2, 0 ) ); 31 var_dump ( substr($str, -2, -3) ); 32 33 $counter++; 34} 35 36/* variation of start and length to point to same element */ 37echo ("\n*** Testing for variations of start and length to point to same element ***\n"); 38var_dump (substr("abcde" , 2, -2) ); 39var_dump (substr("abcde" , -3, -2) ); 40 41/* Testing to return empty string when start denotes the position beyond the truncation (set by negative length) */ 42echo ("\n*** Testing for start > truncation ***\n"); 43var_dump (substr("abcdef" , 4, -4) ); 44 45/* String with null character */ 46echo ("\n*** Testing for string with null characters ***\n"); 47var_dump (substr("abc\x0xy\x0z" ,2) ); 48 49/* String with international characters */ 50echo ("\n*** Testing for string with international characters ***\n"); 51var_dump (substr('\xIñtërnâtiônàlizætiøn',3) ); 52 53/* start <0 && -start > length */ 54echo "\n*** Start before the first char ***\n"; 55var_dump (substr("abcd" , -8) ); 56 57/* Omitting length and passing a NULL length */ 58echo "\n*** Omitting length or using NULL length ***\n"; 59var_dump (substr("abcdef" , 2) ); 60var_dump (substr("abcdef" , 2, NULL) ); 61 62echo"\nDone"; 63 64?> 65--EXPECTF-- 66--- Iteration 1 --- 67 68-- Variations for two arguments -- 69string(0) "" 70string(0) "" 71string(0) "" 72 73-- Variations for three arguments -- 74string(0) "" 75string(0) "" 76string(0) "" 77string(0) "" 78string(0) "" 79string(0) "" 80string(0) "" 81string(0) "" 82string(0) "" 83 84--- Iteration 2 --- 85 86-- Variations for two arguments -- 87string(4) "2345" 88string(5) "12345" 89string(2) "45" 90 91-- Variations for three arguments -- 92string(3) "234" 93string(0) "" 94string(1) "2" 95string(3) "123" 96string(0) "" 97string(2) "12" 98string(2) "45" 99string(0) "" 100string(0) "" 101 102--- Iteration 3 --- 103 104-- Variations for two arguments -- 105string(5) "bcdef" 106string(6) "abcdef" 107string(2) "ef" 108 109-- Variations for three arguments -- 110string(3) "bcd" 111string(0) "" 112string(2) "bc" 113string(3) "abc" 114string(0) "" 115string(3) "abc" 116string(2) "ef" 117string(0) "" 118string(0) "" 119 120--- Iteration 4 --- 121 122-- Variations for two arguments -- 123string(5) "23abc" 124string(6) "123abc" 125string(2) "bc" 126 127-- Variations for three arguments -- 128string(3) "23a" 129string(0) "" 130string(2) "23" 131string(3) "123" 132string(0) "" 133string(3) "123" 134string(2) "bc" 135string(0) "" 136string(0) "" 137 138--- Iteration 5 --- 139 140-- Variations for two arguments -- 141string(6) "123abc" 142string(7) "_123abc" 143string(2) "bc" 144 145-- Variations for three arguments -- 146string(3) "123" 147string(0) "" 148string(3) "123" 149string(3) "_12" 150string(0) "" 151string(4) "_123" 152string(2) "bc" 153string(0) "" 154string(0) "" 155 156*** Testing for variations of start and length to point to same element *** 157string(1) "c" 158string(1) "c" 159 160*** Testing for start > truncation *** 161string(0) "" 162 163*** Testing for string with null characters *** 164string(6) "c%0xy%0z" 165 166*** Testing for string with international characters *** 167string(26) "ñtërnâtiônàlizætiøn" 168 169*** Start before the first char *** 170string(4) "abcd" 171 172*** Omitting length or using NULL length *** 173string(4) "cdef" 174string(4) "cdef" 175 176Done 177