PDA

View Full Version : preg_match


robojob
09-15-2007, 09:35 PM
i found this code that checks that an email address is valid

return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;

how can i make it allow email addresses with a hyphen?

Nick_Burnes
09-15-2007, 10:04 PM
This will make sure the username follows the RFC regulations for email addresses.
!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)

This is a more advanced email validation function. It will check the username, domain name and TLD

To use it just call the following.


if (check_email_address($email)) {
echo $email . ' is a valid email address.';
} else {
echo $email . ' is not a valid email address.';
}


function check_email_address($email) {

// Initial validation

if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
return false;
}

// Split into three sections of the email address sections

$email_array = explode("@", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
// Allow valid domains and ips only

if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false;
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}

Digicoder
09-15-2007, 10:10 PM
i found this code that checks that an email address is valid

return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;

how can i make it allow email addresses with a hyphen?

this code already allows hyphens to be used.