deefer Posted May 3, 2012 Share Posted May 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
trq Posted May 3, 2012 Share Posted May 3, 2012 What are the *different* errors? Quote Link to comment Share on other sites More sharing options...
deefer Posted May 3, 2012 Author Share Posted May 3, 2012 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" Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 3, 2012 Share Posted May 3, 2012 Check out filter_var with the FILTER_VALIDATE_EMAIL/FILTER_VALIDATE_URL flags. Quote Link to comment Share on other sites More sharing options...
xyph Posted May 3, 2012 Share Posted May 3, 2012 Also, if you use your delimiter directly in your expression, it should be escaped with a backslash Quote Link to comment Share on other sites More sharing options...
deefer Posted May 3, 2012 Author Share Posted May 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
xyph Posted May 3, 2012 Share Posted May 3, 2012 \" http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double Quote Link to comment Share on other sites More sharing options...
deefer Posted May 3, 2012 Author Share Posted May 3, 2012 \" http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.double Sorry, I am just not getting this, do you mean it should be $email_array = explode("\@", $email); ? My brain is scrambled after trying to get this fixed for the past 4 hours Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 3, 2012 Share Posted May 3, 2012 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); } Quote Link to comment Share on other sites More sharing options...
deefer Posted May 3, 2012 Author Share Posted May 3, 2012 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 ) Quote Link to comment Share on other sites More sharing options...
xyph Posted May 3, 2012 Share Posted May 3, 2012 If you're not interested in learning, and/or you just want it done now, post in the Freelance forum. Quote Link to comment Share on other sites More sharing options...
deefer Posted May 3, 2012 Author Share Posted May 3, 2012 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 Quote Link to comment Share on other sites More sharing options...
xyph Posted May 4, 2012 Share Posted May 4, 2012 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. Quote Link to comment Share on other sites More sharing options...
deefer Posted May 4, 2012 Author Share Posted May 4, 2012 OK, I have had a nights sleep 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; } ?> Quote Link to comment Share on other sites More sharing options...
deefer Posted May 4, 2012 Author Share Posted May 4, 2012 Got it fixed now, thanks for your help. Quote Link to comment Share on other sites More sharing options...
Andy-H Posted May 4, 2012 Share Posted May 4, 2012 OK, I have had a nights sleep 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. Quote Link to comment Share on other sites More sharing options...
deefer Posted May 4, 2012 Author Share Posted May 4, 2012 OK, I have had a nights sleep 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.