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-- 152Deprecated: Using ${var} in strings is deprecated, use {$var} instead in %s on line %d 153 154*** Testing Miscellaneous input data *** 155string(3) "qqq" 156int(5) 157array(3) { 158 [0]=> 159 string(3) "qqq" 160 [1]=> 161 string(0) "" 162 [2]=> 163 string(3) "ccc" 164} 165int(6) 166 167-- Testing objects -- 168string(12) "Hello, world" 169int(1) 170 171-- Testing arrays -- 172string(15) "multimultimulti" 173int(3) 174string(3) "qqq" 175int(3) 176array(2) { 177 [0]=> 178 string(3) "qqq" 179 [1]=> 180 string(3) "ccc" 181} 182int(6) 183str_replace(): Argument #2 ($replace) must be of type string when argument #1 ($search) is a string 184array(2) { 185 [0]=> 186 string(3) "111" 187 [1]=> 188 string(3) "bbb" 189} 190int(3) 191array(2) { 192 [0]=> 193 string(4) "aaa3" 194 [1]=> 195 string(4) "2bbb" 196} 197int(1) 198 199-- Testing Resources -- 200str_replace(): Argument #3 ($subject) must be of type array|string, resource given 201str_replace(): Argument #3 ($subject) must be of type array|string, resource given 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 223Warning: Undefined variable $strS in %s on line %d 224string(0) "" 225string(5) "FOUND" 226string(5) "FOUND" 227