class person {
var $name;
var $email;
//Getters
function get_name() { return $this->name; }
function get_email() { return $this->email; }
//Setters
function set_name( $name ) { $this->name = $name; }
function set_email( $email ) {
if ( !eregi("^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])*$", $email ) ) {
return false;
} else {
$this->email = $email;
return true;
}
}//EOM set_email
}//EOC person
class person { var $name; var $email; //Getters function get_name() { return $this->name; }
Share
It’s a class that stores a username and email address. The set_email() method checks the supplied address to ensure it looks valid before storing it.
The eregi function checks the email address using a regular expression. These are very powerful ways of perform string manipulation and parsing, but that particular example probably isn’t the best introduction. If you’re just getting started with using regular expressions, you might want to look at Perl compatible regular expressions as these are more widely used and are more powerful. In addition, the ereg functions will be deprecated from PHP5.3+
Here’s one source of introductory information, and I’d recommend using an app like Regex Coach for playing around and testing regular expressions.
To break it down:
Writing a regex to match 100% of all valid email addresses is rather complex, and this is a simplified pattern which will match a majority. Here’s a good article on writing email address regex patterns.