Jump to content
MakeWebGames

php 7.0 preg_match replacement of ereg


boionfire81

Recommended Posts

Ok, here is the code after changing the ereg to preg_match

 

function valid_email($email) {
 // First, we check that there's one @ symbol, and that the lengths are right
 if (!preg_match("^[^@]{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 (!preg_match("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|("[^(\\|")]{0,62}"))$", $local_array[$i])) {
     return false;
   }
 }  
 if (!preg_match("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
   $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 (!preg_match("^(([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;
}

 

This is for one of my new mod purchases. phpcodecheck return no issues found. But when the form is submitted it returns:

A critical error has occurred, and page execution has stopped. Below are the details:

PHP Warning: preg_match(): Unknown modifier '@' (2)

Link to comment
Share on other sites

Well, the regular expressions used there are a bit silly anyway.

You need to add delimiters to the expression, making the expression; /^[^@]{1,64}@[^@]{1,255}$/

But the more sensible solution would be to use filter_var().

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
   return false;
}

Also, quick edit. The source code for FILTER_VALIDATE_EMAIL does a much more comprehensive check on the email. Source.

Edited by IllegalPigeon
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...