Please tell me how to use the various symbols used for expression matching in preg_match
function of PHP.
Besides basic information, please give example for checking a valid e-mail address and a string that should not contain / : ; { } * &
Please tell me how to use the various symbols used for expression matching in preg_match
function of PHP.
Besides basic information, please give example for checking a valid e-mail address and a string that should not contain / : ; { } * &
this is the best set of introductory regex information I've seen recently.
Jeff Atwood recently had an article on his coding horror blog about regular expressions. Check out "Regular Expressions for Regular Programmers".
To check for a valid mail you can either use build in functionality (filter_var()/FILTER_VALIDATE_EMAIL) or use nice ready to use libraries which are compliant to the current RFC. PHP : Parsing Email Adresses in PHP. For examples on preg_match() you can go to the php website and a full list regular expression options is available on Wikipedia. To learn about Regex I recommend "The Regex Coach".
Simple example.. Verifying $var which is a string verifying and checking for (a to z AND A to Z) characters.
<?php
$var = 'hello';
if (ereg("[a-zA-Z]", $var)) {
echo 'it was typed correctly';
} else {
echo 'it was not typed correctly';
}
?>
more regular expressions syntax exemples: http://www.regexlib.com/
EDIT:
if (ereg("^([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)) {
echo 'email ok';
} else {
echo 'email not ok';
}
Regards.
I know the question is about PHP, but my purpose is to illustrate the intricacies of email address validation: To check if an email address conforms to RFC 2822, you can use the Perl module Email::Address. Do take a look at the source of that module as well as the RFC.