0
0
PHPprogramming~20 mins

String replace functions in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Replace Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
?>
Axx
Babcabc
Cxabc
Dxxabc
Attempts:
2 left
💡 Hint
str_replace replaces all occurrences of the search string.
Predict Output
intermediate
2: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;
?>
AHi hello World!
Bhello World!
CHello World!
DHi World!
Attempts:
2 left
💡 Hint
str_ireplace ignores case when searching.
Predict Output
advanced
2: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;
?>
AI like birds and dogs.
BI like birds and fish.
CI like cats and dogs.
DI like fish and birds.
Attempts:
2 left
💡 Hint
When arrays are used, replacements happen pairwise.
Predict Output
advanced
2: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;
?>
Axhelxloxlo
Bhello
Cxhello
DSyntaxError
Attempts:
2 left
💡 Hint
Replacing an empty string inserts the replacement between every character and at the start and end.
Predict Output
expert
3: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;
?>
Aacc
Bccc
Cbcc
Dbbc
Attempts:
2 left
💡 Hint
Inner str_replace runs first, then outer one.