Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is regex a good way to test a url

Tags:

regex

url

php

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?

like image 220
Berming Avatar asked Aug 04 '10 18:08

Berming


People also ask

How do I check if a URL is valid in regex?

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.

Is using regex a good practice?

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.

What is a good URL to match regex?

[a-zA-Z]{2,3})+)(/(.)

How do you validate a URL?

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.


8 Answers

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!';
    }
}
like image 100
BoltClock Avatar answered Oct 24 '22 17:10

BoltClock


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.

like image 25
Mchl Avatar answered Oct 24 '22 16:10

Mchl


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.

like image 37
brabster Avatar answered Oct 24 '22 16:10

brabster


RegExLib is good place to go for Reg Ex expressions

http://www.regexlib.com/Search.aspx?k=URL

like image 37
Conrad Frix Avatar answered Oct 24 '22 15:10

Conrad Frix


What I would do:

  1. Check that the URL is valid using a very open regex or filer_var with FILTER_VALIDATE_URL.
  2. Do an 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.

like image 41
NikiC Avatar answered Oct 24 '22 17:10

NikiC


There are a bunch of 'check that an external file exists' functions on the file_exists() manual page.

like image 40
Tim Lytle Avatar answered Oct 24 '22 16:10

Tim Lytle


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/

like image 33
lando Avatar answered Oct 24 '22 16:10

lando


Pinging a URL to see if it is a valid URL is nonsense!

  • What if host is down?
  • What if the domain is not ping-able?

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.

like image 28
resmo Avatar answered Oct 24 '22 17:10

resmo