[SOLVED] Faulty Expression

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
obi_wasabi
Forum Newbie
Posts: 15
Joined: Thu Dec 16, 2004 12:30 pm
Location: CA

[SOLVED] Faulty Expression

Post by obi_wasabi »

Code: Select all

<?
$email1 = "steve@codejunkie.co.uk";
$email2 = "not a@valid email";

$regexp = "^ ([a-zA-Z0-9._-]+)@([a-zA-Z0-9-])+(\.[a-zA-Z0-9-]+)+$";

if (eregi($regexp, $email1)) {
    print "E-mail address '$email1' is valid <br>\n";
} else {
    print "E-mail address '$email1' is invalid <br>\n";
}

if (eregi($regexp, $email2)) {
    print "E-mail address '$email2' is valid <br>\n";
} else {
    print "E-mail address '$email2' is invalid <br>\n";
}
?>
The regular expression is returning invalid for both, but the first is valid. What did I goof?
Running php 4.3.4
MAC OS 10.2.8
localHost
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Remove the space preceding the first "(" from $regexp so it looks like:

Code: Select all

$regexp = "^([a-zA-Z0-9._-]+)@([a-zA-Z0-9-])+(\.[a-zA-Z0-9-]+)+$";
The carrot signifies the beginning of the string; therefor any email address without exactly one space at the beginning will return false.

Hope this helps.
Last edited by nigma on Tue Apr 19, 2005 7:04 pm, edited 2 times in total.
obi_wasabi
Forum Newbie
Posts: 15
Joined: Thu Dec 16, 2004 12:30 pm
Location: CA

Faulty Expression

Post by obi_wasabi »

nigma wrote:Remove the prepended space between the "^" and the "(" from $regexp so it looks like:

Code: Select all

$regexp = "^([a-zA-Z0-9._-]+)@([a-zA-Z0-9-])+(\.[a-zA-Z0-9-]+)+$";
The carrot signifies the beginning of the string; therefor any email address without exactly one space at the beginning will return false.

Hope this helps.
Worked like a charm, pardon the cliche.
Post Reply