Jump to content

Recommended Posts

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

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]
<?php
function 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.
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]

<?php

function 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]
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.
I made some changes to it so check it out again at http://lszanto.com/email.php

This is the new source code of the check_email funciton.
[code]
<?php

function 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:).
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.
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.
  • 4 weeks later...
×
×
  • 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.