String replace functions help you change parts of text easily. They let you swap old words or characters with new ones.
String replace functions in PHP
str_replace(search, replace, subject, &count);
search is what you want to find in the text.
replace is what you want to put instead.
subject is the text you want to change.
count is optional and tells how many replacements were done.
$newText = str_replace('cat', 'dog', 'The cat is cute.');
$newText = str_replace(['a', 'e'], ['1', '2'], 'apple');
$count = 0; $newText = str_replace('is', 'was', 'This is it. This is fun.', $count); echo $count;
This program shows how to replace words step by step in a sentence.
<?php // Original text $text = "Hello world! PHP is fun."; // Replace 'world' with 'everyone' $newText = str_replace('world', 'everyone', $text); // Replace multiple words $newText2 = str_replace(['PHP', 'fun'], ['Python', 'awesome'], $newText); // Show results echo $text . "\n"; echo $newText . "\n"; echo $newText2 . "\n"; ?>
str_replace is case-sensitive. Use str_ireplace for case-insensitive replacement.
If search is an array, replace should be an array of the same size.
The original string is not changed unless you assign the result back to it.
Use string replace functions to swap parts of text easily.
You can replace single or multiple words or characters at once.
Remember to assign the result to a variable to keep the changes.
Practice
Solution
Step 1: Understand the purpose of str_replace()
The function str_replace() replaces all occurrences of a search string with a replacement string in a given text.Step 2: Compare with other functions
substr() extracts part of a string, strpos() finds position of a substring, strlen() returns string length. None replace text.Final Answer:
str_replace() -> Option BQuick Check:
Replace text = str_replace() [OK]
- Confusing str_replace with substr
- Using strpos instead of replace
- Expecting strlen to replace text
Solution
Step 1: Check the correct parameter order of str_replace()
The correct order is str_replace(search, replace, subject). So 'cat' is search, 'dog' is replace, $text is subject.Step 2: Validate each option
str_replace('cat', 'dog', $text); matches correct order. replace('cat', 'dog', $text); uses a non-existent function. str_replace($text, 'cat', 'dog'); swaps parameters incorrectly. str_replace('dog', 'cat', $text); swaps search and replace.Final Answer:
str_replace('cat', 'dog', $text); -> Option DQuick Check:
str_replace(search, replace, subject) [OK]
- Swapping search and replace
- Using wrong function name
- Incorrect parameter order
$text = "I love apples.";
$result = str_replace("apples", "oranges", $text);
echo $result;Solution
Step 1: Understand str_replace behavior
str_replace replaces all occurrences of the search string with the replacement string in the given text.Step 2: Apply replacement to the string
"apples" is replaced by "oranges" in "I love apples.", resulting in "I love oranges.".Final Answer:
I love oranges. -> Option AQuick Check:
Replace 'apples' with 'oranges' = I love oranges. [OK]
- Expecting original string unchanged
- Not assigning result to variable
- Syntax errors in code
$text = "Hello world!";
str_replace('world', 'PHP', $text);
echo $text;Solution
Step 1: Check str_replace usage
The str_replace call has the correct three arguments: search='world', replace='PHP', subject=$text.Step 2: Understand string immutability in PHP
str_replace returns a new string; it does not change the original string $text unless assigned back, e.g., $text = str_replace('world', 'PHP', $text);Final Answer:
str_replace does not modify $text directly -> Option CQuick Check:
Assign result to variable to update string [OK]
- Not assigning result back to variable
- Assuming str_replace changes string in place
Solution
Step 1: Understand str_replace with arrays
str_replace accepts arrays for search and replace to perform multiple replacements in one call.Step 2: Analyze each option
$result = str_replace(['red', 'green'], ['blue', 'yellow'], $text); uses arrays correctly. $result = str_replace('red', 'blue', $text); str_replace('green', 'yellow', $text); performs first replacement but second call discards result and uses original $text, so 'green' remains. $result = str_replace('red', 'blue', $text) && str_replace('green', 'yellow', $text); uses && which returns boolean, not string. $result = str_replace('red' => 'blue', 'green' => 'yellow', $text); uses invalid syntax.Final Answer:
$result = str_replace(['red', 'green'], ['blue', 'yellow'], $text); -> Option AQuick Check:
Use arrays in str_replace for multiple replacements [OK]
- Using multiple str_replace without chaining
- Using logical operators instead of chaining
- Wrong syntax for arrays in str_replace
Ready to prove it?
Pass the test and this pattern moves unknown
