Annotation of embedaddon/php/ext/standard/tests/strings/str_replace_variation3.phpt, revision 1.1.1.2
1.1 misho 1: --TEST--
2: Test str_replace() function
3: --INI--
4: precision=14
5: --FILE--
6: <?php
7: /*
8: Prototype: mixed str_replace(mixed $search, mixed $replace,
9: mixed $subject [, int &$count]);
10: Description: Replace all occurrences of the search string with
11: the replacement string
12: */
13:
14:
15: echo "\n*** Testing Miscelleneous input data ***\n";
16: /* If replace has fewer values than search, then an empty
17: string is used for the rest of replacement values */
18: var_dump( str_replace(array("a", "a", "b"),
19: array("q", "q"),
20: "aaabb", $count
21: )
22: );
23: var_dump($count);
24: var_dump( str_replace(array("a", "a", "b"),
25: array("q", "q"),
26: array("aaa", "bbb", "ccc"),
27: $count
28: )
29: );
30: var_dump($count);
31:
32:
33: echo "\n-- Testing objects --\n";
34: /* we get "Catchable fatal error: saying Object of class could not be converted
35: to string" by default, when an object is passed instead of string:
1.1.1.2 ! misho 36: The error can be avoided by choosing the __toString magix method as follows: */
1.1 misho 37:
38: class subject
39: {
40: function __toString() {
41: return "Hello, world";
42: }
43: }
44: $obj_subject = new subject;
45:
46: class search
47: {
48: function __toString() {
49: return "Hello, world";
50: }
51: }
52: $obj_search = new search;
53:
54: class replace
55: {
56: function __toString() {
57: return "Hello, world";
58: }
59: }
60: $obj_replace = new replace;
61:
62: var_dump(str_replace("$obj_search", "$obj_replace", "$obj_subject", $count));
63: var_dump($count);
64:
65:
66: echo "\n-- Testing arrays --\n";
67: var_dump(str_replace(array("a", "a", "b"), "multi", "aaa", $count));
68: var_dump($count);
69:
70: var_dump(str_replace( array("a", "a", "b"),
71: array("q", "q", "c"),
72: "aaa", $count
73: )
74: );
75: var_dump($count);
76:
77: var_dump(str_replace( array("a", "a", "b"),
78: array("q", "q", "c"),
79: array("aaa", "bbb"),
80: $count
81: )
82: );
83: var_dump($count);
84:
85: var_dump(str_replace("a", array("q", "q", "c"), array("aaa"), $count));
86: var_dump($count);
87:
88: var_dump(str_replace("a", 1, array("aaa", "bbb"), $count));
89: var_dump($count);
90:
91: var_dump(str_replace(1, 3, array("aaa1", "2bbb"), $count));
92: var_dump($count);
93:
94:
95: echo "\n-- Testing Resources --\n";
96: $resource1 = fopen( __FILE__, "r" );
97: $resource2 = opendir( "." );
98: var_dump(str_replace("stream", "FOUND", $resource1, $count));
99: var_dump($count);
100: var_dump(str_replace("stream", "FOUND", $resource2, $count));
101: var_dump($count);
102:
103:
104: echo "\n-- Testing a longer and heredoc string --\n";
105: $string = <<<EOD
106: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
107: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
108: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
109: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
110: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
111: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
112: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
113: @#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
114: abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789
115: EOD;
116:
117: var_dump( str_replace("abcdef", "FOUND", $string, $count) );
118: var_dump( $count );
119:
120: echo "\n-- Testing a heredoc null string --\n";
121: $str = <<<EOD
122: EOD;
123: var_dump( str_replace("", "FOUND", $str, $count) );
124: var_dump( $count );
125:
126:
127: echo "\n-- Testing simple and complex syntax strings --\n";
128: $str = 'world';
129:
130: /* Simple syntax */
131: var_dump( str_replace("world", "FOUND", "$str") );
132: var_dump( str_replace("world'S", "FOUND", "$str'S") );
133: var_dump( str_replace("worldS", "FOUND", "$strS") );
134:
135: /* String with curly braces, complex syntax */
136: var_dump( str_replace("worldS", "FOUND", "${str}S") );
137: var_dump( str_replace("worldS", "FOUND", "{$str}S") );
138:
139:
140: fclose($resource1);
141: closedir($resource2);
142:
143: ?>
144: ===DONE===
145: --EXPECTF--
146: *** Testing Miscelleneous input data ***
147: string(3) "qqq"
148: int(5)
149: array(3) {
150: [0]=>
151: string(3) "qqq"
152: [1]=>
153: string(0) ""
154: [2]=>
155: string(3) "ccc"
156: }
157: int(6)
158:
159: -- Testing objects --
160: string(12) "Hello, world"
161: int(1)
162:
163: -- Testing arrays --
164: string(15) "multimultimulti"
165: int(3)
166: string(3) "qqq"
167: int(3)
168: array(2) {
169: [0]=>
170: string(3) "qqq"
171: [1]=>
172: string(3) "ccc"
173: }
174: int(6)
175:
176: Notice: Array to string conversion in %s on line %d
177: array(1) {
178: [0]=>
179: string(15) "ArrayArrayArray"
180: }
181: int(3)
182: array(2) {
183: [0]=>
184: string(3) "111"
185: [1]=>
186: string(3) "bbb"
187: }
188: int(3)
189: array(2) {
190: [0]=>
191: string(4) "aaa3"
192: [1]=>
193: string(4) "2bbb"
194: }
195: int(1)
196:
197: -- Testing Resources --
198: string(%d) "Resource id #%d"
199: int(0)
200: string(%d) "Resource id #%d"
201: int(0)
202:
203: -- Testing a longer and heredoc string --
204: string(623) "FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
205: FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
206: FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
207: FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
208: FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
209: FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
210: FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789
211: @#$%^&**&^%$#@!~:())))((((&&&**%$###@@@!!!~~~~@###$%^&*
212: FOUNDghijklmnopqrstuvwxyz0123456789FOUNDghijklmnopqrstuvwxyz0123456789"
213: int(16)
214:
215: -- Testing a heredoc null string --
216: string(0) ""
217: int(0)
218:
219: -- Testing simple and complex syntax strings --
220: string(5) "FOUND"
221: string(5) "FOUND"
222:
223: Notice: Undefined variable: strS in %s on line %d
224: string(0) ""
225: string(5) "FOUND"
226: string(5) "FOUND"
227: ===DONE===
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>