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?
Match the given URL with the regular expression. In Java, this can be done by using Pattern. matcher(). Return true if the URL matches with the given regular expression, else return false.
Yes - regular expressions work very well for input validation. However, often times it's a very good idea to abstract these things away as much as possible as other methods - or even sometimes special validator objects.
[a-zA-Z]{2,3})+)(/(.)
When you create a URL input with the proper type value, url , you get automatic validation that the entered text is at least in the correct form to potentially be a legitimate URL. This can help avoid cases in which the user mis-types their web site's address, or provides an invalid one.
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:
if (filter_var($url, FILTER_VALIDATE_URL) !== false)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_exec($ch);
$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status_code >= 200 && $status_code < 400)
{
echo 'URL is valid!';
}
}
For validation http://www.php.net/manual/en/filter.filters.validate.php
For checking if it exists... well you need to try to access it actually.
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.
RegExLib is good place to go for Reg Ex expressions
http://www.regexlib.com/Search.aspx?k=URL
What I would do:
filer_var
with FILTER_VALIDATE_URL
.file_get_contents
on the url and check that $http_response_header[0]
contains a 200
HTTP resoponse.Now, that's dirty, sure there is some more elegant version using curl and stuff.
There are a bunch of 'check that an external file exists' functions on the file_exists()
manual page.
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/
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.
<?php
$ip = gethostbyname('www.example.com');
echo $ip;
?>
But even if this fails URL can be valid. It just have no DNS entry. So it depends on your needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With