How can you replace a specific group of characters in a text using preg_replace in PHP?

To replace a specific group of characters in a text using preg_replace in PHP, you can use a regular expression pattern to match the characters you want to replace and then use preg_replace to replace them with the desired replacement text. Make sure to use the appropriate regular expression pattern to target the specific group of characters you want to replace.

$text = "Hello world!";
$pattern = '/world/';
$replacement = 'PHP';
$new_text = preg_replace($pattern, $replacement, $text);
echo $new_text; // Output: Hello PHP!