What are some best practices for efficiently replacing substrings in PHP strings?

When replacing substrings in PHP strings, it is important to use the str_replace() function efficiently to avoid unnecessary overhead. One best practice is to use an array to store the search and replace strings if there are multiple replacements needed. This can help reduce the number of function calls and improve performance.

// Example of efficiently replacing substrings in PHP strings
$string = "Hello, World!";
$replacements = array(
    "Hello" => "Hi",
    "World" => "Universe"
);

$newString = str_replace(array_keys($replacements), array_values($replacements), $string);

echo $newString; // Output: Hi, Universe!