What are the potential benefits of using preg_replace or preg_replace_callback in PHP for replacing email addresses?

When dealing with text that contains email addresses, it is common to want to replace or mask them for privacy or security reasons. Using preg_replace or preg_replace_callback in PHP allows you to easily search for email addresses using regular expressions and replace them with a desired string or perform a custom replacement operation. This can be useful when sanitizing user input, anonymizing data, or implementing email obfuscation techniques.

$text = "Contact us at john.doe@example.com for more information.";
$masked_text = preg_replace('/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/', '***REDACTED***', $text);

echo $masked_text;