What are the key differences between str_replace, ereg_replace, eregi_replace, and preg_replace functions in PHP?

The key differences between str_replace, ereg_replace, eregi_replace, and preg_replace functions in PHP lie in the pattern matching capabilities and the type of regular expressions they support. str_replace is a simple string replacement function that does not support regular expressions. ereg_replace and eregi_replace are deprecated functions that use POSIX regular expressions, with eregi_replace being case-insensitive. preg_replace is the recommended function that uses Perl-compatible regular expressions, offering more advanced pattern matching capabilities.

// Using preg_replace to replace a pattern in a string
$original_string = "Hello, World!";
$pattern = "/hello/i"; // case-insensitive pattern
$replacement = "Hi";
$new_string = preg_replace($pattern, $replacement, $original_string);
echo $new_string; // Output: Hi, World!