Annotation of embedaddon/php/ext/standard/tests/strings/strstr.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test strstr() function
3: --FILE--
4: <?php
5: /* Prototype: string strstr ( string $haystack, string $needle );
6: Description: Find first occurrence of a string
7: and reurns the rest of the string from that string
8: */
9:
10: echo "*** Testing basic functionality of strstr() ***\n";
11: var_dump( strstr("test string", "test") );
12: var_dump( strstr("test string", "string") );
13: var_dump( strstr("test string", "strin") );
14: var_dump( strstr("test string", "t s") );
15: var_dump( strstr("test string", "g") );
16: var_dump( md5(strstr("te".chr(0)."st", chr(0))) );
17: var_dump( strstr("tEst", "test") );
18: var_dump( strstr("teSt", "test") );
19: var_dump( @strstr("", "") );
20: var_dump( @strstr("a", "") );
21: var_dump( @strstr("", "a") );
22:
23:
24: echo "\n*** Testing strstr() with various needles ***";
25: $string =
26: "Hello world,012033 -3.3445 NULL TRUE FALSE\0 abcd\xxyz \x000 octal\n
27: abcd$:Hello world";
28:
29: /* needles in an array to get the string starts with needle, in $string */
30: $needles = array(
31: "Hello world",
32: "WORLD",
33: "\0",
34: "\x00",
35: "\x000",
36: "abcd",
37: "xyz",
38: "octal",
39: "-3",
40: -3,
41: "-3.344",
42: -3.344,
43: NULL,
44: "NULL",
45: "0",
46: 0,
47: TRUE,
48: "TRUE",
49: "1",
50: 1,
51: FALSE,
52: "FALSE",
53: " ",
54: " ",
55: 'b',
56: '\n',
57: "\n",
58: "12",
59: "12twelve",
60: $string
61: );
62:
63: /* loop through to get the string starts with "needle" in $string */
64: for( $i = 0; $i < count($needles); $i++ ) {
65: echo "\n-- Iteration $i --\n";
66: var_dump( strstr($string, $needles[$i]) );
67: }
68:
69:
70: echo "\n*** Testing Miscelleneous input data ***\n";
71:
72: echo "-- Passing objects as string and needle --\n";
73: /* we get "Catchable fatal error: saying Object of class needle could not be
74: converted to string" by default when an object is passed instead of string:
75: The error can be avoided by chosing the __toString magix method as follows: */
76:
77: class string
78: {
79: function __toString() {
80: return "Hello, world";
81: }
82: }
83: $obj_string = new string;
84:
85: class needle
86: {
87: function __toString() {
88: return "world";
89: }
90: }
91: $obj_needle = new needle;
92:
93: var_dump(strstr("$obj_string", "$obj_needle"));
94:
95:
96: echo "\n-- passing an array as string and needle --\n";
97: $needles = array("hello", "?world", "!$%**()%**[][[[&@#~!");
98: var_dump( strstr($needles, $needles) ); // won't work
99: var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[1]") ); // works
100: var_dump( strstr("hello?world,!$%**()%**[][[[&@#~!", "$needles[2]") ); // works
101:
102:
103: echo "\n-- passing Resources as string and needle --\n";
104: $resource1 = fopen(__FILE__, "r");
105: $resource2 = opendir(".");
106: var_dump( strstr($resource1, $resource1) );
107: var_dump( strstr($resource1, $resource2) );
108:
109:
110: echo "\n-- Posiibilities with null --\n";
111: var_dump( strstr("", NULL) );
112: var_dump( strstr(NULL, NULL) );
113: var_dump( strstr("a", NULL) );
114: var_dump( strstr("/x0", "0") ); // Hexadecimal NUL
115:
116: echo "\n-- A longer and heredoc string --\n";
117: $string = <<<EOD
118: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
119: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
120: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
121: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
122: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
123: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
124: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
125: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
126: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
127: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
128: EOD;
129: var_dump( strstr($string, "abcd") );
130: var_dump( strstr($string, "1234") );
131:
132: echo "\n-- A heredoc null string --\n";
133: $str = <<<EOD
134: EOD;
135: var_dump( strstr($str, "\0") );
136: var_dump( strstr($str, NULL) );
137: var_dump( strstr($str, "0") );
138:
139:
140: echo "\n-- simple and complex syntax strings --\n";
141: $needle = 'world';
142:
143: /* Simple syntax */
144: var_dump( strstr("Hello, world", "$needle") ); // works
145: var_dump( strstr("Hello, world'S", "$needle'S") ); // works
146: var_dump( strstr("Hello, worldS", "$needleS") ); // won't work
147:
148: /* String with curly braces, complex syntax */
149: var_dump( strstr("Hello, worldS", "${needle}S") ); // works
150: var_dump( strstr("Hello, worldS", "{$needle}S") ); // works
151:
152:
153: echo "\n-- complex strings containing other than 7-bit chars --\n";
154: $str = chr(0).chr(128).chr(129).chr(234).chr(235).chr(254).chr(255);
155: echo "- Positions of some chars in the string '$str' are as follows -\n";
156: echo chr(128)." => ";
157: var_dump( strstr($str, chr(128)) );
158: echo chr(255)." => ";
159: var_dump( strstr($str, chr(255)) );
160: echo chr(256)." => ";
161: var_dump( strstr($str, chr(256)) );
162:
163: echo "\n*** Testing error conditions ***";
164: var_dump( strstr($string, ""));
165: var_dump( strstr() ); // zero argument
166: var_dump( strstr("") ); // null argument
167: var_dump( strstr($string) ); // without "needle"
168: var_dump( strstr("a", "b", "c") ); // args > expected
169: var_dump( strstr(NULL, "") );
170:
171: echo "\nDone";
172:
173: fclose($resource1);
174: closedir($resource2);
175: ?>
176: --EXPECTF--
177: *** Testing basic functionality of strstr() ***
178: string(11) "test string"
179: string(6) "string"
180: string(6) "string"
181: string(8) "t string"
182: string(1) "g"
183: string(32) "7272696018bdeb2c9a3f8d01fc2a9273"
184: bool(false)
185: bool(false)
186: bool(false)
187: bool(false)
188: bool(false)
189:
190: *** Testing strstr() with various needles ***
191: -- Iteration 0 --
192: string(86) "Hello world,012033 -3.3445 NULL TRUE FALSE abcd\xxyz 0 octal
193:
194: abcd$:Hello world"
195:
196: -- Iteration 1 --
197: bool(false)
198:
199: -- Iteration 2 --
200: string(40) "