I'm trying to test the validity of a url entered with php5. I thought of using regex, but assuming that it works correctly all the time, it only solves the problem of the url being syntactically valid. It doesn't tell me anything about the url being correct or working.
I'm trying to find another solution to do both if possible. Or is it better to find 2 separate solutions for this?
If a regex is the way to go, what tested regexes exist for urls?
What I would do:
filer_var
withFILTER_VALIDATE_URL
.file_get_contents
on the url and check that$http_response_header[0]
contains a200
HTTP resoponse.Now, that's dirty, sure there is some more elegant version using curl and stuff.
RegExLib is good place to go for Reg Ex expressions
http://www.regexlib.com/Search.aspx?k=URL
i would use regex to go about solving this problem and i hate regex. This tool however makes my life so much easier... check it out >> http://gskinner.com/RegExr/
Instead of cracking my head over a regex (URLs are very complicated), I just use
filter_var()
, and then attempt to ping the URL using cURL:In order to test that a URL is 'correct or working', you'll need to actually try and interact with it (like a web browser would, for example).
I'd recommend an HTTP library for Perl like LWP::Simple to do so.
Pinging a URL to see if it is a valid URL is nonsense!
If you really want to do a "live" testing, better try to resolve the URL by using DSN. DNS is more reliable then PING or HTTP.
But even if this fails URL can be valid. It just have no DNS entry. So it depends on your needs.