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