Forum Moderators: coopster
I am trying to build a good server side url validator for my website. So far I have done this.
Examples of valid urls
* $url = "https://user:pass@www.example.com:8080/login.php?do=login&style=%23#pagetop";
* $url = "http://user@www.example.com/#pagetop";
* $url = "https://example.com/index.html";
* $url = "ftp://user:****@example.com:21/";
* $url = "http://example.com/index.html/";
*/
function validateURL($url){
// Syntax of a valid url
$urlSyntax = "^(https?¦ftp)\:\/\/([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),
;?&=\$_.-]+)?@)?[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*(\:[0-9]{2,5})?(\/([a-z0-9+\$
_-]\.?)+)*\/?(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?(#[a-z_.-][a-z0-9+\$_.-]*)?\$";
$urlType = false; // By default URL is bad.
// Validate
if (eregi($urlSyntax, $url)){
$urlType = true; //When URL is good.
return $urlType;
}
return $urlType;
}
- This does not accept www.yahoo.com as a valid url. Does any one how to over come this issue in this code?
- More over, do you know any other type of URL that I should consider?
Any help will be appreciated.
Thank you,
[edited by: eelixduppy at 6:55 am (utc) on Jan. 8, 2010]
[edit reason] switched to example.com [/edit]
^(https*¦ftp)
* being zero or more, you may not have an s. That's indeed a monster regexp, too ill ATM to give you a better one and test it . . .
Second, eregi is deprecated, to be eliminated in v6, use preg_match, a delimiter, and add the i modifier (case insensitive) for HTTP
if (preg_match("/$urlSyntax/i", $url)){
Works exactly the same.
Last, you don't want to escape the last $. It means "end of string" just like ^ means beginning. So you might want to do
$urlSyntax = '(https*¦ftp) . . . ect. to end ... [a-z0-9+\$_.-]*)?';
then
if (preg_match("/^$urlSyntax$/i", $url)){