What potential pitfalls should be considered when using regular expressions to modify email addresses in PHP?

One potential pitfall when using regular expressions to modify email addresses in PHP is not accounting for all possible valid email formats. It is important to thoroughly test the regular expression pattern to ensure it accurately captures all valid email address variations. Additionally, care should be taken to avoid inadvertently modifying valid email addresses or introducing errors during the modification process.

$email = 'example@email.com';

// Check if email is valid before modifying
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
    // Modify email address using regular expression
    $modifiedEmail = preg_replace('/@/', '_at_', $email);
    
    echo $modifiedEmail;
} else {
    echo 'Invalid email address';
}