Re: Regex for Validating URL
| From: | Wouter van Vliet | Date: | Tue, 07 Sep 2004 12:49:48 +0000 |
| Subject: | Re: Regex for Validating URL | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-196028@lists.php.net to get a copy of this message | ||
On Tue, 7 Sep 2004 10:58:48 +0300, Burhan Khalid <phplist@meidomus.com> wrote:
> -----Original Message-----
> From: Nick Wilson [mailto:nick@stylesheet.org]
> Sent: Thursday, September 02, 2004 11:59 AM
>
> Hi all,
>
> yeah, i know, i did do quite a bit of searching but I just cant find it...
>
> Does anyone have the regex to make sure an http address is full and without
> error? like http://www.example.com
>
> http://www.php.net/parse_url should do what you require. If
> all you want to
> do is filter out the different parts of the url and check if they exist or
> not.
Consider what you'd mark as a full url, do you want to accept ftp://
and such links as well.. quite generic, I'd go for smth like:
/^([a-z]+):\/\/(([\w\.]+)\.([a-z]{2,3}))(:(\d+))?(\/.*)?$/
Which would give you:
Array
(
[0] => ftp://www.example.com.com.uk:88/something/to/remember
[1] => ftp
[2] => www.example.com.com.uk
[3] => www.example.com.com
[4] => uk
[5] => :88
[6] => 88
[7] => /something/to/remember
)
Here're seme things I didn't take into account:
* double dots (www...example..com)
* double slashes (www.example.com//asdfasf//asdfasf)
* check for protocol validity, might wanna change the first part to
(ftp|http|chrome|.. any protocol you want to accept)
* I left everything after the first slash completely to the
imagination of your input data - this can contain pretty much anything
anyways
* login details (user:password@domain.example.com)
If you want to have a full regex that is really complete, check out:
http://www.foad.org/~abigail/Perl/url3.pl,
and execute it .. (linked
from http://www.foad.org/~abigail/Perl/url2.html)
Enjoy!