lszanto Posted January 28, 2007 Share Posted January 28, 2007 I've been working on a way to validate emails without using regex, and I have come up with the following soulition which can be found [url=http://lszanto.com/email.php]here[/url] comments would be nice and if you have found a way of tricking it into believeing it is an email address please advise me. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/ Share on other sites More sharing options...
trq Posted January 28, 2007 Share Posted January 28, 2007 You might want to post the actual code if you want any feedback. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171078 Share on other sites More sharing options...
ShogunWarrior Posted January 28, 2007 Share Posted January 28, 2007 [b]kkj.../@hhh.com[/b] is valid Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171116 Share on other sites More sharing options...
Nameless12 Posted January 28, 2007 Share Posted January 28, 2007 I don't mean to hijack this guys thread but he has not pasted his code yet and I was thinking doing this earlier today but thought my time would be better spent elsewhere, I finally did it and here is the result[code]<?phpfunction is_email($email){ $size = strlen($email); $explode = explode('@', $email); if (sizeof($explode) == 2) { if (strstr($explode[1], '.')) { $end = explode('.', $explode[1]); $domain = $end[0]; $end = $end[1]; if ($end > 5) return false; } for ($n = 0; $n < $size; ++$n) { if ($email[$n] == '/' || $email[$n] == '\\' || $email[$n] == ';' || $email[$n] == '#' || $email[$n] == '"' || $email[$n] == "'" || $email[$n] == '!' || $email[$n] == '%' || $email[$n] == '^' || $email[$n] == '*' || $email[$n] == '(' || $email[$n] == ')' || $email[$n] == '{' || $email[$n] == '}' || $email[$n] == '+' || $email[$n] == '~' || $email[$n] == '?' || $email[$n] == '&' || $email[$n] == '<' || $email[$n] == '>' || $email[$n] == ',' || $email[$n] == '`' || $email[$n] == "\n" || $email[$n] == "\t" || $email[$n] == "\r") return false; } if (strlen($domain) > 2 && strlen($explode[0]) > 2) return true; } return false;}?>[/code]what did i learn from this?? that i will continue to use regex unless I feel like making it a php c extension. Anyway izanto post your code when you get around to it i would like to see how you did yours. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171122 Share on other sites More sharing options...
alpine Posted January 28, 2007 Share Posted January 28, 2007 As i don't see the point in doing this without using regex, i still found it to be an amusing idea.None of the above one does the trick, neither will my example - anyhow, here it is:[code]<?phpfunction noregex_email($email){$main = explode('@', $email);if(count($main) <> 2){ return false;}$domain = explode('.',$main[1]);if(!in_array(count($domain), array(2,3))){ return false;}foreach($domain as $dom){ if(strlen($dom) < 2){ return false; }}$valid = array_merge(range('a','z'), range('0','9'), array('-','_','.'));foreach(str_split(strtolower($email)) as $char){if($char == "@") $char = "a";if(!in_array($char, $valid, true)){ return false;}}return true;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171204 Share on other sites More sharing options...
lszanto Posted January 28, 2007 Author Share Posted January 28, 2007 Sorry I didn't post the code but obviously need to fix the allowing of kkj.../@hhh.com but this is the code I used. [code]function check_email($email) { $parts = explode("@", $email); $name = trim($parts[0]); if(empty($parts[1]) || empty($name)) { return false; } $parts2 = explode(".", $parts[1]); $domain = trim($parts2[0]); $end = trim($parts2[1]); if(empty($end) || empty($domain)) { return false; } return true;}[/code]I'll add a bit more security and update later. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171400 Share on other sites More sharing options...
corbin Posted January 29, 2007 Share Posted January 29, 2007 ^$%#$%-=1234aaaa@a.a.a.a.a.a.comIt validates that....PS weird and off topic question but what does var <> var2 do? I'm guessing equal to or something, but I've never bothered to find out Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171632 Share on other sites More sharing options...
trq Posted January 29, 2007 Share Posted January 29, 2007 <> is the same as !=Not equal. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171634 Share on other sites More sharing options...
lszanto Posted January 29, 2007 Author Share Posted January 29, 2007 I made some changes to it so check it out again at http://lszanto.com/email.phpThis is the new source code of the check_email funciton.[code]<?phpfunction check_email($email) { $parts = explode("@", $email); $name = $parts[0]; $dot = strpos($name, "."); $slash = strpos($name, "/"); $comma = strpos($name, ","); $hash = strpos($name, "#"); $carrot = strpos($name, "^"); $excl = strpos($name, "!"); $dolla = strpos($name, "$"); $pcent = strpos($name, "%"); $amp = strpos($name, "&"); $aster = strpos($name, "*"); $bckt = strpos($name, "("); $bckt2 = strpos($name, ")"); $plus = strpos($name, "+"); $minus = strpos($name, "-"); $equals = strpos($name, "="); if($dot || $slash || $comma || $hash || $carrot || $excl || $dolla || $pcent || $amp || $aster || $bckt || $bckt2 || $plus || $minus || $equals) { return false; } if(empty($parts[1]) || empty($name)) { return false; } $parts2 = explode(".", $parts[1]); $domain = $parts2[0]; $dot = strpos($domain, "."); $slash = strpos($domain, "/"); $comma = strpos($domain, ","); $hash = strpos($domain, "#"); $carrot = strpos($domain, "^"); $excl = strpos($domain, "!"); $dolla = strpos($domain, "$"); $pcent = strpos($domain, "%"); $amp = strpos($domain, "&"); $aster = strpos($domain, "*"); $bckt = strpos($domain, "("); $bckt2 = strpos($domain, ")"); $plus = strpos($domain, "+"); $minus = strpos($domain, "-"); $equals = strpos($domain, "="); $test = strpos($domain, "l"); if($dot > 0 || $slash > 0 || $comma > 0 || $hash > 0 || $carrot > 0 || $excl > 0 || $dolla > 0 || $pcent > 0 || $amp > 0 || $aster > 0 || $bckt > 0 || $bckt2 > 0 || $plus > 0 || $minus > 0 || $equals > 0 ) { return false; } $end = $parts2[1]; $dot = strpos($end, "."); $slash = strpos($end, "/"); $comma = strpos($end, ","); $hash = strpos($end, "#"); $carrot = strpos($end, "^"); $excl = strpos($end, "!"); $dolla = strpos($end, "$"); $pcent = strpos($end, "%"); $amp = strpos($end, "&"); $aster = strpos($end, "*"); $bckt = strpos($end, "("); $bckt2 = strpos($end, ")"); $plus = strpos($end, "+"); $minus = strpos($end, "-"); $equals = strpos($end, "="); if($dot || $slash || $comma || $hash || $carrot || $excl || $dolla || $pcent || $amp || $aster || $bckt || $bckt2 || $plus || $minus || $equals) { return false; } if(empty($end) || empty($domain) || !empty($parts2[2])) { return false; } return true;}?>[/code]Comments if you please:). Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171643 Share on other sites More sharing options...
alpine Posted January 29, 2007 Share Posted January 29, 2007 [b]test@server.co.uk[/b] doesn't pass[b]test@ser;ver.com[/b] passes...and so will many many more - you should start checking against a valid range of chars instead of illegal chars when matching. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171649 Share on other sites More sharing options...
ShogunWarrior Posted January 29, 2007 Share Posted January 29, 2007 This won't even catch the problems:[code]$dot = strpos($name, "."); $slash = strpos($name, "/"); $comma = strpos($name, ","); $hash = strpos($name, "#"); $carrot = strpos($name, "^"); $excl = strpos($name, "!"); $dolla = strpos($name, "$"); $pcent = strpos($name, "%"); $amp = strpos($name, "&"); $aster = strpos($name, "*"); $bckt = strpos($name, "("); $bckt2 = strpos($name, ")"); $plus = strpos($name, "+"); $minus = strpos($name, "-"); $equals = strpos($name, "="); if($dot || $slash || $comma || $hash || $carrot || $excl || $dolla || $pcent || $amp || $aster || $bckt || $bckt2 || $plus || $minus || $equals) { return false; }[/code]If the character '#' is the FIRST character (e.g #john@gmail.com) then it will return 0 and all the others will return FALSE, thus your IF statement will not catch an illegal character at the start of the line. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171917 Share on other sites More sharing options...
makeshift_theory Posted January 29, 2007 Share Posted January 29, 2007 I see the effort but why try and reinvent the proverbial wheel here. To use RegEx here would take less code and less time. The only reason to do otherwise would be optmization as RegEx can hog a server down. With all that processing it almost seems useless to do it without RegEx. I definately appreciate the thinking outside of normal conventions though. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-171975 Share on other sites More sharing options...
AXiSS Posted January 29, 2007 Share Posted January 29, 2007 Is there something wrong with using eregi() to check for a correct email pattern? Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-172258 Share on other sites More sharing options...
ShogunWarrior Posted January 29, 2007 Share Posted January 29, 2007 Nope. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-172281 Share on other sites More sharing options...
AXiSS Posted February 1, 2007 Share Posted February 1, 2007 OK then... is there any benefit to doing all that complicated stuff above over using eregi()? Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-174130 Share on other sites More sharing options...
lszanto Posted February 1, 2007 Author Share Posted February 1, 2007 Nah I was just trying to put off learning regex cause it looked hard but I actually took a look at it and learnt most the basic stuff in a few minutes so I can do stuff properly now. Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-174353 Share on other sites More sharing options...
Lumio Posted February 25, 2007 Share Posted February 25, 2007 My email Adress is not valid :Dlumio.noma@gmx.nethmm.. but I use it every day Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-193654 Share on other sites More sharing options...
AXiSS Posted February 26, 2007 Share Posted February 26, 2007 test@houston.rr.com did not pass. :P Link to comment https://forums.phpfreaks.com/topic/36045-email-checker-without-regex/#findComment-194786 Share on other sites More sharing options...
Recommended Posts