To clarify, my goal here is to make sure that anything that validates can be thrown directly into an <a> tag and clicking that A tag will take you to the intended spot.
I don't want people to type index.html or any other relative url that would point to a file on my site.
this code:
function is_valid_url($url) {
$pattern = "#^(http:\/\/|https:\/\/|www\.)(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(:(\d+))?(\/)*$#i";
if (!preg_match($pattern, $url)) {
return false;
} else {
return true;
}
} // is_valid_url()
$str = '//example.com';
if (is_valid_url($str)) {
echo 'YES, ' . $str . ' is a valid url';
} else {
echo 'NO!, ' . $str . ' is NOT a valid url';
}
produces this:
NO!, //example.com is NOT a valid url
which is good! I wouldn't want that to be considered valid, would I?
I think the 63-char limit sounds good. to which sections does it apply? Is there an RFC for this?
Also, it might also be true that someone would want a URL with a filename or parameters in it like sfullman said.
How would I modify the expression to make the www/http/https optional?