What is the purpose of using regular expressions in PHP for filtering email addresses?

Regular expressions in PHP can be used to filter email addresses by validating their format. This is important for ensuring that the email addresses provided by users are in the correct format before processing them further. By using regular expressions, we can easily check if an email address follows the standard format of "username@domain.com" and reject any invalid input.

$email = "example@example.com";

if (preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/", $email)) {
    echo "Valid email address";
} else {
    echo "Invalid email address";
}