Annotation of embedaddon/php/ext/standard/tests/strings/substr.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Testing substr() function
3: --FILE--
4: <?php
5:
6: /* Prototype: string substr( string str, int start[, int length] )
7: * Description: Returns the portion of string specified by the start and length parameters.
8: */
9:
10: $strings_array = array( NULL, "", 12345, "abcdef", "123abc", "_123abc");
11:
12:
13: /* Testing for error conditions */
14: echo "*** Testing for error conditions ***\n";
15:
16: /* Zero Argument */
17: var_dump( substr() );
18:
19: /* NULL as Argument */
20: var_dump( substr(NULL) );
21:
22: /* Single Argument */
23: var_dump( substr("abcde") );
24:
25: /* Scalar Argument */
26: var_dump( substr(12345) );
27:
28: /* more than valid number of arguments ( valid are 2 or 3 ) */
29: var_dump( substr("abcde", 2, 3, 4) );
30:
31: $counter = 1;
32: foreach ($strings_array as $str) {
33: /* variations with two arguments */
34: /* start values >, < and = 0 */
35:
36: echo ("\n--- Iteration ".$counter." ---\n");
37: echo ("\n-- Variations for two arguments --\n");
38: var_dump ( substr($str, 1) );
39: var_dump ( substr($str, 0) );
40: var_dump ( substr($str, -2) );
41:
42: /* variations with three arguments */
43: /* start value variations with length values */
44:
45: echo ("\n-- Variations for three arguments --\n");
46: var_dump ( substr($str, 1, 3) );
47: var_dump ( substr($str, 1, 0) );
48: var_dump ( substr($str, 1, -3) );
49: var_dump ( substr($str, 0, 3) );
50: var_dump ( substr($str, 0, 0) );
51: var_dump ( substr($str, 0, -3) );
52: var_dump ( substr($str, -2, 3) );
53: var_dump ( substr($str, -2, 0 ) );
54: var_dump ( substr($str, -2, -3) );
55:
56: $counter++;
57: }
58:
59: /* variation of start and length to point to same element */
60: echo ("\n*** Testing for variations of start and length to point to same element ***\n");
61: var_dump (substr("abcde" , 2, -2) );
62: var_dump (substr("abcde" , -3, -2) );
63:
64: /* Testing to return empty string when start denotes the position beyond the truncation (set by negative length) */
65: echo ("\n*** Testing for start > truncation ***\n");
66: var_dump (substr("abcdef" , 4, -4) );
67:
68: /* String with null character */
69: echo ("\n*** Testing for string with null characters ***\n");
70: var_dump (substr("abc\x0xy\x0z" ,2) );
71:
72: /* String with international characters */
73: echo ("\n*** Testing for string with international characters ***\n");
74: var_dump (substr('\xIñtërnâtiônàlizætiøn',3) );
75:
76: /* start <0 && -start > length */
77: echo "\n*** Start before the first char ***\n";
78: var_dump (substr("abcd" , -8) );
79:
80: echo"\nDone";
81:
82: ?>
83: --EXPECTF--
84: *** Testing for error conditions ***
85:
86: Warning: substr() expects at least 2 parameters, 0 given in %s on line %d
87: NULL
88:
89: Warning: substr() expects at least 2 parameters, 1 given in %s on line %d
90: NULL
91:
92: Warning: substr() expects at least 2 parameters, 1 given in %s on line %d
93: NULL
94:
95: Warning: substr() expects at least 2 parameters, 1 given in %s on line %d
96: NULL
97:
98: Warning: substr() expects at most 3 parameters, 4 given in %s on line %d
99: NULL
100:
101: --- Iteration 1 ---
102:
103: -- Variations for two arguments --
104: bool(false)
105: bool(false)
106: bool(false)
107:
108: -- Variations for three arguments --
109: bool(false)
110: bool(false)
111: bool(false)
112: bool(false)
113: bool(false)
114: bool(false)
115: bool(false)
116: bool(false)
117: bool(false)
118:
119: --- Iteration 2 ---
120:
121: -- Variations for two arguments --
122: bool(false)
123: bool(false)
124: bool(false)
125:
126: -- Variations for three arguments --
127: bool(false)
128: bool(false)
129: bool(false)
130: bool(false)
131: bool(false)
132: bool(false)
133: bool(false)
134: bool(false)
135: bool(false)
136:
137: --- Iteration 3 ---
138:
139: -- Variations for two arguments --
140: string(4) "2345"
141: string(5) "12345"
142: string(2) "45"
143:
144: -- Variations for three arguments --
145: string(3) "234"
146: string(0) ""
147: string(1) "2"
148: string(3) "123"
149: string(0) ""
150: string(2) "12"
151: string(2) "45"
152: string(0) ""
153: string(0) ""
154:
155: --- Iteration 4 ---
156:
157: -- Variations for two arguments --
158: string(5) "bcdef"
159: string(6) "abcdef"
160: string(2) "ef"
161:
162: -- Variations for three arguments --
163: string(3) "bcd"
164: string(0) ""
165: string(2) "bc"
166: string(3) "abc"
167: string(0) ""
168: string(3) "abc"
169: string(2) "ef"
170: string(0) ""
171: string(0) ""
172:
173: --- Iteration 5 ---
174:
175: -- Variations for two arguments --
176: string(5) "23abc"
177: string(6) "123abc"
178: string(2) "bc"
179:
180: -- Variations for three arguments --
181: string(3) "23a"
182: string(0) ""
183: string(2) "23"
184: string(3) "123"
185: string(0) ""
186: string(3) "123"
187: string(2) "bc"
188: string(0) ""
189: string(0) ""
190:
191: --- Iteration 6 ---
192:
193: -- Variations for two arguments --
194: string(6) "123abc"
195: string(7) "_123abc"
196: string(2) "bc"
197:
198: -- Variations for three arguments --
199: string(3) "123"
200: string(0) ""
201: string(3) "123"
202: string(3) "_12"
203: string(0) ""
204: string(4) "_123"
205: string(2) "bc"
206: string(0) ""
207: string(0) ""
208:
209: *** Testing for variations of start and length to point to same element ***
210: string(1) "c"
211: string(1) "c"
212:
213: *** Testing for start > truncation ***
214: bool(false)
215:
216: *** Testing for string with null characters ***
217: string(6) "c xy z"
218:
219: *** Testing for string with international characters ***
220: string(26) "ñtërnâtiônàlizætiøn"
221:
222: *** Start before the first char ***
223: string(4) "abcd"
224:
225: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>