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