What is the function preg_replace used for in PHP?

The preg_replace function in PHP is used to perform a search and replace using regular expressions. It allows you to search for a pattern in a string and replace it with another specified value. This function is particularly useful when you need to manipulate text data based on specific patterns or criteria.

// Example of using preg_replace to replace all occurrences of 'apple' with 'orange' in a string
$string = "I like apples and apples are delicious.";
$newString = preg_replace('/apple/', 'orange', $string);
echo $newString;