Jonas
Jonas

Reputation: 311

PHP: need explanation using [a-zA-Z0-9]

I am new to PHP (not programming overall), and having problems with this simple line of code. I want to check whether some input field has been filled as anysymbolornumber@anysymbolornumber just for checking whether correct email was typed. I don't get any error, but the whole check system doesn't work. Here is my code and thanks!

  if ($email = "[a-zA-Z0-9]@[a-zA-Z0-9]")
{

Upvotes: 3

Views: 3575

Answers (6)

hectorg87
hectorg87

Reputation: 753

Another way taken from: http://www.linuxjournal.com/article/9585

function check_email_address($email) {
  // First, we check that there's one @ symbol, 
  // and that the lengths are right.
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters 
    // in one section or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $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;
    }
  }
  // Check if domain is IP. If not, 
  // it should be valid domain name
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    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;
}

Upvotes: 0

DavidO
DavidO

Reputation: 13942

It looks like you're trying to verify that an email address matches a certain pattern. But you're not using the proper function. You probably want something like preg_match( $pattern, $target ).

Also, your regex isn't doing what you would want anyway. In particular, you need some quantifiers, or else your email addresses will only be able to consist of one character ahead of the @, and one after. And you need anchors at the beginning and end of the sequence so that you're matching against the entire address, not just the two characters closest to the @.

Consider this:

if( preg_match("^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+$", $email ) ) {
    // Whatever
}

Keep in mind, however, that this is really a poor-man's approach to validating an email address. Email addresses can contain a lot more characters than those listed in the character class I provided. Furthermore, it would also be possible to construct an invalid email address with those same character classes. It doesn't even begin to deal with Unicode. Using a regex to validate an email address is quite difficult. Friedl takes a shot at it in Mastering Regular Expressions (O'Reilly), and his effort takes a 2KB regular expression pattern. At best, this is only a basic sanity check. It's not a secure means of verifying an email address. At worst, it literally misses valid regexes, and still matches invalid ones.

There is the mailparse_rfc822_parse_addresses function which is more reliable in detecting and matching email addresses.

Upvotes: 2

Fluffeh
Fluffeh

Reputation: 33532

As correctly pointed out in the comments, the regex you are using isn't actually a very good way of validating the email. There are much better ways, but if you are just wanting to get a look at how regular expressions work, it is a starting point. I am not an expert in regex, but this will at least get your if statement working :)

if(preg_match("[a-zA-Z0-9]@[a-zA-Z0-9]",$email)
{
    // Your stuff
}

Upvotes: 2

DonSeba
DonSeba

Reputation: 2119

Since your new to php , i suggest you should buy a book or read an tutorial or two.

For email validation you should use filter_var an build in function that comes with with php 5.2 and up :

<?php
if(!filter_var("[email protected]", FILTER_VALIDATE_EMAIL)){
    echo("E-mail is not valid");
}else{
   echo("E-mail is valid");
}
?> 

Upvotes: 9

Gntem
Gntem

Reputation: 7165

you can use other functions .. instead of regular expressions

if(filter_var($email,FILTER_VALIDATE_EMAIL)){
 echo "Valid email";
}else{
 echo "Not a valid email";
}

Upvotes: 4

Kao
Kao

Reputation: 2272

You need to use preg_match to run the regular expression.

Now you're setting the $email = to the regular expression.

It could look like:

if ( preg_match("[a-zA-Z0-9]@[a-zA-Z0-9]", $email ))

Also keep in mind when matching in an if you must use the == operator.

I believe best pratice would be to use a filter_var instead like:

if( ! filter_var( $email , FILTER_VALIDATE_EMAIL )) {
 // Failed.
}

Upvotes: 0

Related Questions