Bird
Raised Fist0
PHPprogramming~10 mins

String replace functions in PHP - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to replace all occurrences of 'apple' with 'orange' in the string.

PHP
<?php
$text = "I like apple pie.";
$newText = str_replace([1], "orange", $text);
echo $newText;
?>
Drag options to blanks, or click blank then click option'
A"pear"
B"banana"
C"apple"
D"grape"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong string to replace.
Not putting the string to find in quotes.
2fill in blank
medium

Complete the code to replace the first occurrence of 'cat' with 'dog' in the string.

PHP
<?php
$text = "The cat sat on the cat mat.";
$newText = preg_replace('/[1]/', "dog", $text, 1);
echo $newText;
?>
Drag options to blanks, or click blank then click option'
Adog
Bcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using the replacement string as the pattern.
Forgetting the slashes around the pattern.
3fill in blank
hard

Fix the error in the code to replace 'blue' with 'red' in the string.

PHP
<?php
$color = "blue";
$text = "The sky is blue.";
$newText = str_replace([1], "red", $text);
echo $newText;
?>
Drag options to blanks, or click blank then click option'
Ablue
Bcolor
C"color"
D$color
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the dollar sign for variables.
Passing the variable name as a string.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their replacements, then replace them in the text.

PHP
<?php
$text = "I have a cat and a dog.";
$replacements = array([1] => "dog", [2] => "cat");
$newText = str_replace(array_keys($replacements), array_values($replacements), $text);
echo $newText;
?>
Drag options to blanks, or click blank then click option'
A"cat"
B"dog"
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping keys and values.
Not using quotes around the words.
5fill in blank
hard

Fill all three blanks to replace all vowels with '*' in the string using str_replace.

PHP
<?php
$text = "Hello World!";
$vowels = array([1]);
$stars = array([2]);
$newText = str_replace($vowels, $stars, [3]);
echo $newText;
?>
Drag options to blanks, or click blank then click option'
A"a", "e", "i", "o", "u"
B"*", "*", "*", "*", "*"
C$text
D"aeiou"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of an array for vowels or stars.
Forgetting the dollar sign for variables.

Practice

(1/5)
1. Which PHP function is used to replace all occurrences of a string with another string?
easy
A. strpos()
B. str_replace()
C. substr()
D. strlen()

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    str_replace() -> Option B
  4. Quick Check:

    Replace text = str_replace() [OK]
Hint: Remember: str_replace replaces text, others do different tasks [OK]
Common Mistakes:
  • Confusing str_replace with substr
  • Using strpos instead of replace
  • Expecting strlen to replace text
2. Which of the following is the correct syntax to replace 'cat' with 'dog' in the string $text using PHP?
easy
A. str_replace('dog', 'cat', $text);
B. replace('cat', 'dog', $text);
C. str_replace($text, 'cat', 'dog');
D. str_replace('cat', 'dog', $text);

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    str_replace('cat', 'dog', $text); -> Option D
  4. Quick Check:

    str_replace(search, replace, subject) [OK]
Hint: Remember parameter order: search, replace, then subject [OK]
Common Mistakes:
  • Swapping search and replace
  • Using wrong function name
  • Incorrect parameter order
3. What will be the output of the following PHP code?
$text = "I love apples.";
$result = str_replace("apples", "oranges", $text);
echo $result;
medium
A. I love oranges.
B. I love apples.
C. I love apples.oranges
D. Syntax error

Solution

  1. Step 1: Understand str_replace behavior

    str_replace replaces all occurrences of the search string with the replacement string in the given text.
  2. Step 2: Apply replacement to the string

    "apples" is replaced by "oranges" in "I love apples.", resulting in "I love oranges.".
  3. Final Answer:

    I love oranges. -> Option A
  4. Quick Check:

    Replace 'apples' with 'oranges' = I love oranges. [OK]
Hint: str_replace returns new string; echo the result to see changes [OK]
Common Mistakes:
  • Expecting original string unchanged
  • Not assigning result to variable
  • Syntax errors in code
4. Identify the error in the following PHP code snippet:
$text = "Hello world!";
str_replace('world', 'PHP', $text);
echo $text;
medium
A. Missing third argument in str_replace
B. Syntax error in str_replace call
C. str_replace does not modify $text directly
D. echo statement is incorrect

Solution

  1. Step 1: Check str_replace usage

    The str_replace call has the correct three arguments: search='world', replace='PHP', subject=$text.
  2. 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);
  3. Final Answer:

    str_replace does not modify $text directly -> Option C
  4. Quick Check:

    Assign result to variable to update string [OK]
Hint: Assign str_replace result to variable; original string stays same [OK]
Common Mistakes:
  • Not assigning result back to variable
  • Assuming str_replace changes string in place
5. You want to replace multiple words in a string at once: replace 'red' with 'blue' and 'green' with 'yellow' in $text. Which code correctly does this in PHP?
hard
A. $result = str_replace(['red', 'green'], ['blue', 'yellow'], $text);
B. $result = str_replace('red', 'blue', $text); str_replace('green', 'yellow', $text);
C. $result = str_replace('red', 'blue', $text) && str_replace('green', 'yellow', $text);
D. $result = str_replace('red' => 'blue', 'green' => 'yellow', $text);

Solution

  1. Step 1: Understand str_replace with arrays

    str_replace accepts arrays for search and replace to perform multiple replacements in one call.
  2. 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.
  3. Final Answer:

    $result = str_replace(['red', 'green'], ['blue', 'yellow'], $text); -> Option A
  4. Quick Check:

    Use arrays in str_replace for multiple replacements [OK]
Hint: Use arrays in str_replace to replace many words at once [OK]
Common Mistakes:
  • Using multiple str_replace without chaining
  • Using logical operators instead of chaining
  • Wrong syntax for arrays in str_replace
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard