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 12echo "\n*** Testing str_replace() with various subjects ***"; 13$subject = "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE\000 14 \x000\x5ACD\0abcd \xXYZ\tabcd $$@#%^&*!~,.:;?: !!Hello, World 15 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)"; 16 17/* needles in an array to be compared in the string $string */ 18$search_str = array ( 19 "Hello, World", 20 'Hello, World', 21 '!!Hello, World', 22 "??Hello, World", 23 "$@#%^&*!~,.:;?", 24 "123", 25 123, 26 "-1.2345", 27 -1.2344, 28 "abcd", 29 'XYZ', 30 NULL, 31 "NULL", 32 "0", 33 0, 34 "", 35 " ", 36 "\0", 37 "\x000", 38 "\x5AC", 39 "\0000", 40 ".3", 41 TRUE, 42 "TRUE", 43 "1", 44 1, 45 FALSE, 46 "FALSE", 47 " ", 48 " ", 49 'b', 50 '\t', 51 "\t", 52 chr(128).chr(234).chr(65).chr(255).chr(256), 53 $subject 54); 55 56/* loop through to get the $string */ 57for( $i = 0; $i < count($search_str); $i++ ) { 58 echo "\n--- Iteration $i ---"; 59 echo "\n-- String after replacing the search value is => --\n"; 60 var_dump( str_replace($search_str[$i], "FOUND", $subject, $count) ); 61 echo "-- search string has found '$count' times\n"; 62} 63 64?> 65--EXPECT-- 66*** Testing str_replace() with various subjects *** 67--- Iteration 0 --- 68-- String after replacing the search value is => -- 69string(181) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 70 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!FOUND 71 ?FOUND chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 72-- search string has found '2' times 73 74--- Iteration 1 --- 75-- String after replacing the search value is => -- 76string(181) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 77 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!FOUND 78 ?FOUND chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 79-- search string has found '2' times 80 81--- Iteration 2 --- 82-- String after replacing the search value is => -- 83string(186) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 84 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: FOUND 85 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 86-- search string has found '1' times 87 88--- Iteration 3 --- 89-- String after replacing the search value is => -- 90string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 91 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 92 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 93-- search string has found '0' times 94 95--- Iteration 4 --- 96-- String after replacing the search value is => -- 97string(186) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 98 0ZCDabcd \xXYZ abcd $FOUND: !!Hello, World 99 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 100-- search string has found '1' times 101 102--- Iteration 5 --- 103-- String after replacing the search value is => -- 104string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 105 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 106 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 107-- search string has found '0' times 108 109--- Iteration 6 --- 110-- String after replacing the search value is => -- 111string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 112 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 113 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 114-- search string has found '0' times 115 116--- Iteration 7 --- 117-- String after replacing the search value is => -- 118string(193) "Hello, world,0120333.3445FOUND67 NULL TRUE FALSE 119 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 120 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 121-- search string has found '1' times 122 123--- Iteration 8 --- 124-- String after replacing the search value is => -- 125string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 126 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 127 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 128-- search string has found '0' times 129 130--- Iteration 9 --- 131-- String after replacing the search value is => -- 132string(197) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 133 0ZCDFOUND \xXYZ FOUND $$@#%^&*!~,.:;?: !!Hello, World 134 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 135-- search string has found '2' times 136 137--- Iteration 10 --- 138-- String after replacing the search value is => -- 139string(197) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 140 0ZCDabcd \xFOUND abcd $$@#%^&*!~,.:;?: !!Hello, World 141 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 142-- search string has found '1' times 143 144--- Iteration 11 --- 145-- String after replacing the search value is => -- 146string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 147 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 148 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 149-- search string has found '0' times 150 151--- Iteration 12 --- 152-- String after replacing the search value is => -- 153string(196) "Hello, world,0120333.3445-1.234567 FOUND TRUE FALSE 154 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 155 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 156-- search string has found '1' times 157 158--- Iteration 13 --- 159-- String after replacing the search value is => -- 160string(211) "Hello, world,FOUND12FOUND333.3445-1.234567 NULL TRUE FALSE 161 FOUNDZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 162 ?Hello, World chr(FOUND).chr(128).chr(234).chr(65).chr(255).chr(256)" 163-- search string has found '4' times 164 165--- Iteration 14 --- 166-- String after replacing the search value is => -- 167string(211) "Hello, world,FOUND12FOUND333.3445-1.234567 NULL TRUE FALSE 168 FOUNDZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 169 ?Hello, World chr(FOUND).chr(128).chr(234).chr(65).chr(255).chr(256)" 170-- search string has found '4' times 171 172--- Iteration 15 --- 173-- String after replacing the search value is => -- 174string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 175 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 176 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 177-- search string has found '0' times 178 179--- Iteration 16 --- 180-- String after replacing the search value is => -- 181string(335) "Hello,FOUNDworld,0120333.3445-1.234567FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDNULLFOUNDTRUEFOUNDFALSE 182FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUND0ZCDabcdFOUND\xXYZ abcdFOUND$$@#%^&*!~,.:;?:FOUND!!Hello,FOUNDWorld 183FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUND?Hello,FOUNDWorldFOUNDchr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 184-- search string has found '35' times 185 186--- Iteration 17 --- 187-- String after replacing the search value is => -- 188string(207) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSEFOUND 189 FOUND0ZCDFOUNDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 190 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 191-- search string has found '3' times 192 193--- Iteration 18 --- 194-- String after replacing the search value is => -- 195string(198) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 196 FOUNDZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 197 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 198-- search string has found '1' times 199 200--- Iteration 19 --- 201-- String after replacing the search value is => -- 202string(198) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 203 0FOUNDDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 204 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 205-- search string has found '1' times 206 207--- Iteration 20 --- 208-- String after replacing the search value is => -- 209string(198) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 210 FOUNDZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 211 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 212-- search string has found '1' times 213 214--- Iteration 21 --- 215-- String after replacing the search value is => -- 216string(198) "Hello, world,0120333FOUND445-1.234567 NULL TRUE FALSE 217 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 218 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 219-- search string has found '1' times 220 221--- Iteration 22 --- 222-- String after replacing the search value is => -- 223string(207) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE 224 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 225 ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" 226-- search string has found '3' times 227 228--- Iteration 23 --- 229-- String after replacing the search value is => -- 230string(196) "Hello, world,0120333.3445-1.234567 NULL FOUND FALSE 231 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 232 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 233-- search string has found '1' times 234 235--- Iteration 24 --- 236-- String after replacing the search value is => -- 237string(207) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE 238 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 239 ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" 240-- search string has found '3' times 241 242--- Iteration 25 --- 243-- String after replacing the search value is => -- 244string(207) "Hello, world,0FOUND20333.3445-FOUND.234567 NULL TRUE FALSE 245 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 246 ?Hello, World chr(0).chr(FOUND28).chr(234).chr(65).chr(255).chr(256)" 247-- search string has found '3' times 248 249--- Iteration 26 --- 250-- String after replacing the search value is => -- 251string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 252 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 253 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 254-- search string has found '0' times 255 256--- Iteration 27 --- 257-- String after replacing the search value is => -- 258string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FOUND 259 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 260 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 261-- search string has found '1' times 262 263--- Iteration 28 --- 264-- String after replacing the search value is => -- 265string(335) "Hello,FOUNDworld,0120333.3445-1.234567FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDNULLFOUNDTRUEFOUNDFALSE 266FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUND0ZCDabcdFOUND\xXYZ abcdFOUND$$@#%^&*!~,.:;?:FOUND!!Hello,FOUNDWorld 267FOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUNDFOUND?Hello,FOUNDWorldFOUNDchr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 268-- search string has found '35' times 269 270--- Iteration 29 --- 271-- String after replacing the search value is => -- 272string(190) "Hello, world,0120333.3445-1.234567FOUNDNULL TRUE FALSE 273 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 274 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 275-- search string has found '1' times 276 277--- Iteration 30 --- 278-- String after replacing the search value is => -- 279string(203) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 280 0ZCDaFOUNDcd \xXYZ aFOUNDcd $$@#%^&*!~,.:;?: !!Hello, World 281 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 282-- search string has found '2' times 283 284--- Iteration 31 --- 285-- String after replacing the search value is => -- 286string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 287 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 288 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 289-- search string has found '0' times 290 291--- Iteration 32 --- 292-- String after replacing the search value is => -- 293string(199) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 294 0ZCDabcd \xXYZFOUNDabcd $$@#%^&*!~,.:;?: !!Hello, World 295 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 296-- search string has found '1' times 297 298--- Iteration 33 --- 299-- String after replacing the search value is => -- 300string(195) "Hello, world,0120333.3445-1.234567 NULL TRUE FALSE 301 0ZCDabcd \xXYZ abcd $$@#%^&*!~,.:;?: !!Hello, World 302 ?Hello, World chr(0).chr(128).chr(234).chr(65).chr(255).chr(256)" 303-- search string has found '0' times 304 305--- Iteration 34 --- 306-- String after replacing the search value is => -- 307string(5) "FOUND" 308-- search string has found '1' times 309