What is the purpose of the ereg_replace function in PHP and how does it work?

The ereg_replace function in PHP is used to search for a regular expression pattern in a string and replace it with another string. This function is useful for performing pattern-based search and replace operations on strings. It works by taking three parameters: the regular expression pattern to search for, the replacement string, and the input string to search within.

$input_string = "Hello, world!";
$pattern = "/world/";
$replacement = "PHP";
$output_string = ereg_replace($pattern, $replacement, $input_string);

echo $output_string; // Output: Hello, PHP!