Challenge - 5 Problems
String Replace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of str_replace with overlapping replacements
What is the output of the following PHP code?
PHP
<?php $text = "abcabc"; $result = str_replace("abc", "x", $text); echo $result; ?>
Attempts:
2 left
💡 Hint
str_replace replaces all occurrences of the search string.
✗ Incorrect
The function replaces every 'abc' with 'x'. Since 'abc' appears twice, the output is 'xx'.
❓ Predict Output
intermediate2:00remaining
Behavior of str_ireplace with case insensitive replacement
What will be the output of this PHP code?
PHP
<?php $text = "Hello World!"; $result = str_ireplace("hello", "Hi", $text); echo $result; ?>
Attempts:
2 left
💡 Hint
str_ireplace ignores case when searching.
✗ Incorrect
str_ireplace replaces 'hello' ignoring case, so 'Hello' becomes 'Hi'.
❓ Predict Output
advanced2:00remaining
Output of str_replace with array replacements
What is the output of this PHP code snippet?
PHP
<?php $text = "I like cats and dogs."; $result = str_replace(["cats", "dogs"], ["birds", "fish"], $text); echo $result; ?>
Attempts:
2 left
💡 Hint
When arrays are used, replacements happen pairwise.
✗ Incorrect
Each element in the search array is replaced by the corresponding element in the replacement array.
❓ Predict Output
advanced2:00remaining
Result of str_replace when search string is empty
What will this PHP code output?
PHP
<?php $text = "hello"; $result = str_replace("", "x", $text); echo $result; ?>
Attempts:
2 left
💡 Hint
Replacing an empty string inserts the replacement between every character and at the start and end.
✗ Incorrect
Replacing empty string inserts 'x' before, between, and after characters, resulting in 'xhelxloxlo'.
❓ Predict Output
expert3:00remaining
Output of nested str_replace calls
What is the output of this PHP code?
PHP
<?php $text = "abc"; $result = str_replace("a", "b", str_replace("b", "c", $text)); echo $result; ?>
Attempts:
2 left
💡 Hint
Inner str_replace runs first, then outer one.
✗ Incorrect
Inner replaces 'b' with 'c' → 'acc'. Outer replaces 'a' with 'b' → 'bcc'.