What are the potential limitations of using str_replace for multiple string replacements in PHP?
When using str_replace for multiple string replacements in PHP, one potential limitation is that it can only replace one string at a time. To overcome this limitation, you can use str_replace in a loop to replace multiple strings sequentially.
<?php
// Define the strings to be replaced and their replacements
$strings_to_replace = array("apple", "banana", "cherry");
$replacements = array("orange", "grape", "strawberry");
// Input string
$input_string = "I like apple, banana, and cherry.";
// Replace each string sequentially
foreach ($strings_to_replace as $key => $string) {
$input_string = str_replace($string, $replacements[$key], $input_string);
}
// Output the modified string
echo $input_string;
?>