What are some alternative methods to str_replace for manipulating strings in PHP?

One alternative method to str_replace for manipulating strings in PHP is to use the preg_replace function, which allows for more advanced pattern matching using regular expressions. Another option is to use the strtr function, which can replace multiple occurrences of characters in a string simultaneously. Additionally, the substr_replace function can be used to replace a portion of a string with another substring.

// Using preg_replace to replace a pattern in a string
$string = "Hello, World!";
$newString = preg_replace('/Hello/', 'Hi', $string);
echo $newString;

// Using strtr to replace multiple characters in a string
$string = "abcdef";
$replacePairs = array("a" => "1", "b" => "2", "c" => "3");
$newString = strtr($string, $replacePairs);
echo $newString;

// Using substr_replace to replace a portion of a string
$string = "Hello, World!";
$newString = substr_replace($string, "Universe", 7, 5);
echo $newString;