Jump to content

Deprecated: Function ereg() is deprecated (Cannot fix this error)


deefer

Recommended Posts

Hi,

I am getting 5 errors of "Deprecated: Function ereg() is deprecated" in the following code

I have tried replacing with "preg_match//" but it is throwing up different errors.

Please can someone have a look and point me in the right direction.

 

 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;
}

 

Thanks

Link to comment
Share on other sites

one at a time,

if I change the firest ereg to

 

(!preg_match("/^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&
↪'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/",
$local_array[$i])) {
      return false;

 

The form returns "Email field empty or not valid"

Link to comment
Share on other sites

Check out filter_var with the FILTER_VALIDATE_EMAIL/FILTER_VALIDATE_URL flags.

That is way above my level of understanding at the moment :(

 

Now I have

  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;
    }
  }

 

And I get error "Parse error: syntax error, unexpected '@'" in line 7 of the code posted above

Link to comment
Share on other sites

Check out filter_var with the FILTER_VALIDATE_EMAIL/FILTER_VALIDATE_URL flags.

That is way above my level of understanding at the moment :(

 

Now I have

  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;
    }
  }

 

And I get error "Parse error: syntax error, unexpected '@'" in line 7 of the code posted above

 

 

It's way easier than regular expressions:

 

 

if ( filter_var($email, FILTER_VALIDATE_EMAIL) === false )
   // email invalid

 

 

Judging from your code (the return statements) it's the body of a function:

 

 

function email_is_valid($email)
{
   return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}

Link to comment
Share on other sites

ok,

I must be missing something really simple here, because whatever I try just isn't working  :'(

My form is still retuning an invalid mail address and everything I do results in more errors, right now I think it would be better to pay someone to fix it and then I can see what is done. (it's either that or I might end up breaking my laptop  :confused:)

Link to comment
Share on other sites

If you're not interested in learning, and/or you just want it done now, post in the Freelance forum.

I am definately interested in learning, it's now been 5 hours and I don't seem to be getting anywhere.

Serious frustration is setting in as I have to be at work in 8 hours and still need to sleep. Would really like to get this fixed first though.

I really appreciate your help, and welcome more advice, I am just not understanding most of it as I am a novice  :D

Link to comment
Share on other sites

You should use an editing program that allows syntax highlighting. You would notice your error. Notepad++ is a free one, with a PHP plug-in.

 

Your issue is that you're escaping an ending double-quote somewhere in your code. This causes your code to be interpreted as a string, until the next double quote... Anything after that double-quote will be interpreted as PHP code, and since it's probably a string ('@'), it will throw an error.

Link to comment
Share on other sites

OK, I have had a nights sleep  :D

I reuploaded the original file, and went back to step one again.

 

I then changed the first instance of ereg to preg_match and added / deliminators and now the error moves on to the next case or ereg

But now, if I use (!preg_match(" I get error

Warning: preg_match() [function.preg-match]: Unknown modifier '_'

 

Code starts line 123

function contact(){
	$settings = $this->defaultSettings;
	$rows = MyActiveRecord::FindAll("settings");
	foreach ($rows as $r){
		$settings[$r->name] = $r->value; 
	}

	$error = array();
	//$error[] = "<div class='error'>Please fill the <strong>Name</strong> field</div>";

	if (strlen($_POST['submit'])){

		if (strlen(trim(strip_tags($_POST['name']))) < 1){
			$error[] = "<div class='error'>Please fill the <strong>Name</strong> field</div>";
		}

		if (strlen(trim(strip_tags($_POST['email']))) < 1 || !check_email_address(trim(strip_tags($_POST['email'])))){
			$error[] = "<div class='error'><strong>Email</strong> field empty or not valid</div>";
		}

		if (strlen(trim(strip_tags($_POST['msg']))) < 1 ){
			$error[] = "<div class='error'>Please fill the <strong>Message</strong> field</div>";
		}

		if (sizeof($error) < 1){

			$mail = new PHPMailer();
			$mail->IsHTML(true);
			$mail->SetLanguage("en", 'language/');
			$mail->From = trim(strip_tags($_POST['email']));
			$mail->FromName = 'Mobile site';
			$mail->AddAddress($settings["contact_email"]);
			$mail->Subject = "Msg from mobile site";
			$body = trim(strip_tags($_POST['msg']));

			$AltBody = trim(strip_tags($_POST['msg']));

			$mail->Body = $body;
			$mail->$AltBody = $AltBody;
			$mail->Send();

			$error[] = "<div class='ok'>Message sent</div>";
			$_POST = array();

		}

	}

	include_once("pages/header.php");
	include_once("pages/contact.php");
	include_once("pages/footer.php");
}

///////////////////////////////////////////////////////////////////////////////////		


}//end class c_ajax



function check_email_address($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;
    }
  }
  // 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;
}




?>

Link to comment
Share on other sites

OK, I have had a nights sleep  :D

I reuploaded the original file, and went back to step one again.

 

I then changed the first instance of ereg to preg_match and added / deliminators and now the error moves on to the next case or ereg

But now, if I use (!preg_match(" I get error

Warning: preg_match() [function.preg-match]: Unknown modifier '_'

 

Code starts line 123

function contact(){
      $settings = $this->defaultSettings;
      $rows = MyActiveRecord::FindAll("settings");
      foreach ($rows as $r){
         $settings[$r->name] = $r->value; 
      }
      
      $error = array();
      //$error[] = "<div class='error'>Please fill the <strong>Name</strong> field<>";
      
      if (strlen($_POST['submit'])){
      
         if (strlen(trim(strip_tags($_POST['name']))) < 1){
            $error[] = "<div class='error'>Please fill the <strong>Name</strong> field<>";
         }
      
         if (strlen(trim(strip_tags($_POST['email']))) < 1 || !check_email_address(trim(strip_tags($_POST['email'])))){
            $error[] = "<div class='error'><strong>Email</strong> field empty or not valid<>";
         }
      
         if (strlen(trim(strip_tags($_POST['msg']))) < 1 ){
            $error[] = "<div class='error'>Please fill the <strong>Message</strong> field<>";
         }
         
         if (sizeof($error) < 1){
            
            $mail = new PHPMailer();
            $mail->IsHTML(true);
            $mail->SetLanguage("en", 'language/');
            $mail->From = trim(strip_tags($_POST['email']));
            $mail->FromName = 'Mobile site';
            $mail->AddAddress($settings["contact_email"]);
            $mail->Subject = "Msg from mobile site";
            $body = trim(strip_tags($_POST['msg']));
            
            $AltBody = trim(strip_tags($_POST['msg']));
            
            $mail->Body = $body;
            $mail->$AltBody = $AltBody;
            $mail->Send();
            
            $error[] = "<div class='ok'>Message sent<>";
            $_POST = array();
            
         }
      
      }
      
      include_once("pages/header.php");
      include_once("pages/contact.php");
      include_once("pages/footer.php");
   }
      
///////////////////////////////////////////////////////////////////////////////////      


}//end class c_ajax



function check_email_address($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;
    }
  }
  // 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;
}




?>

 

 

As I said numerous times, changing your function to:

 

 


function check_email_address($email) {
   return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}

 

 

would have sufficed.

 

 

Link to comment
Share on other sites

OK, I have had a nights sleep  :D

I reuploaded the original file, and went back to step one again.

 

I then changed the first instance of ereg to preg_match and added / deliminators and now the error moves on to the next case or ereg

But now, if I use (!preg_match(" I get error

Warning: preg_match() [function.preg-match]: Unknown modifier '_'

 

Code starts line 123

function contact(){
      $settings = $this->defaultSettings;
      $rows = MyActiveRecord::FindAll("settings");
      foreach ($rows as $r){
         $settings[$r->name] = $r->value; 
      }
      
      $error = array();
      //$error[] = "<div class='error'>Please fill the <strong>Name</strong> field<>";
      
      if (strlen($_POST['submit'])){
      
         if (strlen(trim(strip_tags($_POST['name']))) < 1){
            $error[] = "<div class='error'>Please fill the <strong>Name</strong> field<>";
         }
      
         if (strlen(trim(strip_tags($_POST['email']))) < 1 || !check_email_address(trim(strip_tags($_POST['email'])))){
            $error[] = "<div class='error'><strong>Email</strong> field empty or not valid<>";
         }
      
         if (strlen(trim(strip_tags($_POST['msg']))) < 1 ){
            $error[] = "<div class='error'>Please fill the <strong>Message</strong> field<>";
         }
         
         if (sizeof($error) < 1){
            
            $mail = new PHPMailer();
            $mail->IsHTML(true);
            $mail->SetLanguage("en", 'language/');
            $mail->From = trim(strip_tags($_POST['email']));
            $mail->FromName = 'Mobile site';
            $mail->AddAddress($settings["contact_email"]);
            $mail->Subject = "Msg from mobile site";
            $body = trim(strip_tags($_POST['msg']));
            
            $AltBody = trim(strip_tags($_POST['msg']));
            
            $mail->Body = $body;
            $mail->$AltBody = $AltBody;
            $mail->Send();
            
            $error[] = "<div class='ok'>Message sent<>";
            $_POST = array();
            
         }
      
      }
      
      include_once("pages/header.php");
      include_once("pages/contact.php");
      include_once("pages/footer.php");
   }
      
///////////////////////////////////////////////////////////////////////////////////      


}//end class c_ajax



function check_email_address($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;
    }
  }
  // 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;
}




?>

 

 

As I said numerous times, changing your function to:

 

 


function check_email_address($email) {
   return (bool)filter_var($email, FILTER_VALIDATE_EMAIL);
}

 

 

would have sufficed.

 

Hi,

I tried this, but just couldn't get it to work. I am a total novice to PHP, so was probably not putting it in the right place, or not removing the old code correctly, or just something else daft.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.