PHP Matching Valid E-Mail Address – String Function

<?php
$regex = "/^[\w\d!#$%&'*+-\/=?^'{|}~]+(\.[\w\d!#$%&'*+-\/=?^'{|}~]+)*@([a-z\d][-a-z\d]*[a-z\d]\.)+[a-z][-a-z\d]*[a-z]$/"; $values = array(
"user@example.com",
"user@example"
); foreach ($values as $value) {
    if (preg_match($regex, $value)) {
        printf("Found valid address: %s\n", $value);
    } else {
        printf("INVALID address: %s\n", $value);
    }
} ?>

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.