Page 1 of 1

ereg to preg

Posted: Tue Mar 09, 2010 5:46 am
by aravona
I've got some code to validate email / phone , but it was using ereg but have changed it to use preg as ereg is deprecated.

This worked before (with ereg) but now it constantly get 'your email / number is invalid' this is my php with my functions in it:

Code: Select all

<?php 
function validate_telephone_number($tel, $formats) 
{ 
$format = trim(preg_replace('/[^0-9]/', '#', $tel)); 
 
return (in_array($format, $formats)) ? true : false; 
} 
 
$formats = array('#####-######', '(#####)######', 
'(#####) ######', '##### ######', '###########'); 
 
function check_email_address($to) {
  if (!preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}¦[0-9]{1,3})(\]?)$/', $to)) {
    return false;
  }
  $email_array = explode("@", $to);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
    if
(!preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}¦[0-9]{1,3})(\]?)$/',
$local_array[$i])) {
      return false;
    }
  }
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) {
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false;
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if
(!preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}¦[0-9]{1,3})(\]?)$/',
$domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
?>
Any help would be appriciated.

Thanks,

Aravona.

Re: ereg to preg

Posted: Tue Mar 09, 2010 10:15 am
by akuji36
This will helpyou a little.

A few video tutorials from Nettus

http://blog.themeforest.net/screencasts ... pressions/

and

http://www.bestechvideos.com/2009/04/02 ... or-dummies

Jeff Way's are very helpful

thanks

Rod

Re: ereg to preg

Posted: Tue Mar 09, 2010 10:15 am
by akuji36
This should help you :

A few video tutorials from Nettus

http://blog.themeforest.net/screencasts ... pressions/

and

http://www.bestechvideos.com/2009/04/02 ... or-dummies

Jeff Way's Php videos are very helpful.

thanks

Rod

Re: ereg to preg

Posted: Tue Mar 09, 2010 2:37 pm
by tr0gd0rr
Also, be careful with email address validation. Per [url=viewtopic.php?f=50&t=109132]this January devnetwork thread[url], `2@to` is theoretically a valid email address.

Personally I find it annoying when a web site won't let me use a + in my email address to take advantage of Gmail's email address plussing feature.

Re: ereg to preg

Posted: Wed Mar 10, 2010 1:16 am
by aravona
Well the code simply takes your email and sends you information. If you don't put in a valid email you wont get the information so it would be a bit silly not to, this is so that people do fill the form out properly.

If Gmail allows +'s I'll probs change my preg - I use gmail and but I didnt know it allowed it :)