Page 1 of 1

Valid email/website

Posted: Wed Jan 05, 2005 3:04 pm
by Todd_Z
Hey, I'm looking for a function that tells me whether something is a valid email address or a valid website address - just to give a true or false value. I'm using

Code: Select all

<?php
  $email = eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $str);
?>
for checking if its a valid email address, but what do i use for a valid website? Thanks.

Posted: Wed Jan 05, 2005 9:57 pm
by Hibba
What about this:

Code: Select all

<?php 
function is_url($url) { 
    if (!preg_match('#^http\\:\\/\\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url)) {
        return false; 
    } else { 
        return true; 
    } 
} 

if (is_url("http://www.tsn.dk")) { 
    echo "Yes, that's an URL alright"; 
} else { 
    echo "Doesn't seem to be a valid URL"; 
} 
?>

Posted: Wed Jan 05, 2005 10:49 pm
by Todd_Z
Great thank you ~ To clean up that code:

Code: Select all

<?php

function is_url($url) {
    return !preg_match('#^http\:\/\/[a-z0-9\-]+\.([a-z0-9\-]+\.)?[a-z]+#i', $url);
}

?>

Posted: Wed Jan 05, 2005 10:55 pm
by feyd
the pattern can be cleaned up a bit more...

Code: Select all

<?php

function is_url($url) {
    return !preg_match('#^http://[a-z0-9-]+\.([a-z0-9-]+\.)?[a-z]+#i', $url);
}

?>
however, that's only vaguely valid urls...

Posted: Wed Jan 05, 2005 10:58 pm
by Hibba
True, if you want the action to actually see if it is a valid working URL, then you would need something like this:

Code: Select all

<?php 
function is_url($url) { 
    $fp = @fopen($url,"r"); 
    if ($fp) { 
        fclose($fp); 
        return true; 
    } else { 
        return false; 
    } 
} 
?>