I am looking for a way to validate a URL. I don't need to check that it actually exists as in the example below this- I just need to know that its feasible that its a real URL.
For example, it must begin with either http:// or ftp:// (yes thats ok because I dont want https or mailto or other links), then it must contain a string, then it would have to have a period, followed by at least 2 characters.
I'm guessing I will have to use regular expressions, but I dont know exactly how to go about this.
-Thanks in advance.
http://us2.php.net/manual/en/function.ereg-replace.php has the following code, but it does not work for www. It must have http:// or ftp:// at the beginning.
Must start with either http(with an optional s):// or ftp:// followed by any number of alphanumeric characters and the understrike (this allows for subdomains, etc) followed by an ending that is at least 2 characters long.
Note: if you do not want it to allow https:// just remove the (s?) or if you want to add mailto: just add it in with a new | after the ftp://
Hey, it's not my RegExp, so don't go saying that I suck at them! I was just giving Brendandonhue a suggestion... I didn't even know if it worked or not. lol I anticipated you'd come in here and tell me that I did something wrong.
I'm not the world's greatest PHP programmer, as can plainly be seen here, so don't expect my suggestions to be solutions all the time.
Jona
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
((w+.){1,}) means to match one or more occurence of an alphanumeric (and the _) character followed by a period, one or more time. ie. test. or test.test.test. will both validate. This is so that http://infinitypages.com or http://your.very.own.subdomain.infinitypages.com will both validate (as they are valid addresses)
w{2,}$ means $url must end with two or more alphanumeric and the understrike characters... So, this will NOT allow for http://www.infinitypages.com/home.php... Wasn't sure if you wanted it to.
Originally posted by Jona ... you'd come in here and tell me that I did something wrong.
Sorry. Didn't mean it that way. As brendandonhue said, I just wanted to provide a better alternative, as the other regexp would't really do all that much to validate a URL
Yeah, I know. I wasn't upset or anything--besides, it's your job lol. But like I said, I knew you'd come over here and say something about it. No offense taken whatsoever.
Jona
Visit Slightly Remarkable to see my portfolio, resumé, and consulting rates.
I built a regular expression for URIs about the same time I made one for e-mails. This one comes from RFC2396 Uniform Resource Identifiers (URI): Generic Syntax. Because it is generic syntax, it does not test for any specific scheme, which is good and bad. Good: allows for the many valid schemes currently in existence, and others to be created later. Bad: a non-valid scheme will not be recognized.
But anyway, it is attached. Since this one is far more complicated and lengthy than the e-mail, I've also attached the outlines for each part. It should help you to break it down, or build another one up if you choose to.
I have not found any bugs, but if anyone else does please tell me.
Bookmarks