21

I've seen many questions but wasn't able to understand how it works as I want a more simple case.

If we have text, whatever it is, I'd like to check if it is a URL or not.

$text = "something.com"; //this is a url

if (!IsUrl($text)){
    echo "No it is not url";
    exit; // die well
}else{
    echo "Yes it is url";
    // my else codes goes
}

function IsUrl($url){
    // ???
}

Is there any other way rather than checking with JavaScript in the case JS is blocked?

Fozi
  • 4,973
  • 1
  • 32
  • 56
Reham Fahmy
  • 4,937
  • 15
  • 50
  • 71

8 Answers8

42

The code below worked for me:

if(filter_var($text, FILTER_VALIDATE_URL))
{
    echo "Yes it is url";
    exit; // die well
}
else
{
    echo "No it is not url";
   // my else codes goes
}

You can also specify RFC compliance and other requirements on the URL using flags. See PHP Validate Filters for more details.

user3078359
  • 627
  • 1
  • 6
  • 4
27

PHP's filter_var function is what you need. Look for FILTER_VALIDATE_URL. You can also set flags to fine-tune your implementation.
No regex needed....

Bhavik Shah
  • 2,300
  • 1
  • 17
  • 32
enygma
  • 684
  • 4
  • 6
  • 2
    However this is only true if you use `http` in front of the URL. For instance, www.google.com will not be validated correctly. – Patrick Reck Aug 05 '14 at 10:34
  • 7
    Technically, according to the definition of a URL (or URI) it requires a protocol (http, https, etc) to be the first thing in the string. What you're describing is just the domain and path portions of the full URL. – enygma Aug 06 '14 at 01:45
  • www.google.com i think is not a complete url. But if you want a smart method. use the regex. – Juni Brosas Nov 27 '15 at 12:47
19

http://www.php.net/manual/en/function.preg-match.php#93824

<?php 
    $regex = "((https?|ftp)\:\/\/)?"; // SCHEME 
    $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass 
    $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP 
    $regex .= "(\:[0-9]{2,5})?"; // Port 
    $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path 
    $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query 
    $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor 

       if(preg_match("/^$regex$/i", $url)) // `i` flag for case-insensitive
       { 
               return true; 
       } 
?>

but your example URL is over simplified, (\w+)\.(\w+) would match it. somebody else mentioned filter_var which is simply a filter_var($url, FILTER_VALIDATE_URL) but it doesn't seem to like non-ascii characters so, beware...

Community
  • 1
  • 1
JKirchartz
  • 17,612
  • 7
  • 60
  • 88
  • 1
    I would recommend looking into the GET Query. Didn't parse a youtube link. I need to make some changes to make them work based on: http://stackoverflow.com/questions/23959352/validate-url-query-string-with-regex – Maurice Klimek May 03 '16 at 11:25
7

Check if it is a valid url (example.com IS NOT a valid URL)

    function isValidURL($url)
    {
        return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*
        (:[0-9]+)?(/.*)?$|i', $url);
    }

How to use the function:

    if(!isValidURL($fldbanner_url))
    {
        $errMsg .= "* Please enter valid URL including http://<br>";
    }

Source: http://phpcentral.com/208-url-validation-in-php.html

Sindre
  • 3,880
  • 2
  • 26
  • 39
4

Regexes are a poor way to validate something as complex as a URL.

PHP's filter_var() function offers a much more robust way to validate URLs. Plus, it's faster, since it's native code.

greenie2600
  • 1,679
  • 3
  • 17
  • 24
  • 3
    Huh? This question isn't about ASP.net or C#. It's asking specifically how to validate a URL in PHP. – greenie2600 Mar 20 '15 at 19:51
  • If you look above, the original question does not mention anything about PHP. If anything it's asking how to achieve the outcome without the use of JS. So in actual fact, your answer is not appropriate for the question asked. Furthermore, the answer Jack approved should not have been given the green tick, as it's a PHP answer, to a non-PHP question. Yet another example of poor moderation IMO. – Fandango68 Mar 24 '15 at 01:00
  • 5
    The question is tagged with "php". The example code provided is PHP. The asker probably should have been more explicit with his question—but it's clear from context that he's trying to validate the URL server-side, and is using PHP as his server-side language. And I am now done with this silly argument :) – greenie2600 Mar 25 '15 at 01:31
2

You could use the following regex pattern to check if your variable is an url or not :

$pattern = "\b(([\w-]+:\/\/?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|\/)))";
2

I don't think there is a definitive answer to this. Example of a valid URL:

localhost
http://xxx.xxx.xxx/alkjnsdf
abs.com

If you have some text. and not a large amount of it. You can check by doing a CURL request and see if that returns a valid response. Otherwise if I put localhost, it could be a link and it could be something else and you wouldn't be able check it.

Churk
  • 4,556
  • 5
  • 22
  • 37
  • yes i don't care if it working or not i just want to make sure it isn't like $text = "something blah blah blah"; – Reham Fahmy Mar 08 '12 at 18:48
  • Try some of the solutions other have posted, but just because it passes the regex doesn't mean it is a valid url such as http://a.com/a – Churk Mar 08 '12 at 18:49
  • Churk has hit the nail on the head. Your requirements for a "valid URL" are too lose to provide an answer. It sounds like you just want to make sure the input looks like it could possibly be a URL or host name. If that's the case, the only thing you can really do is make sure it doesn't contain any spaces. – A.J. Brown Mar 08 '12 at 19:02
1

Something like might work for you:

$arr = array('abc.com/foo',
'localhost',
'abc+def',
'how r u',
'https://how r u',
'ftp://abc.com',
'a.b');
foreach ($arr as $u) {
   $url = $u;
   if (!preg_match('#^(?:https?|ftp)://#', $url, $m))
      $url = 'http://' . $url;
   echo "$u => ";
   var_dump(filter_var($url, FILTER_VALIDATE_URL));
}

OUTPUT:

abc.com/foo => string(18) "http://abc.com/foo"
localhost => string(16) "http://localhost"
abc+def => string(14) "http://abc+def"
how r u => bool(false)
https://how r u => bool(false)
ftp://abc.com => string(13) "ftp://abc.com"
a.b => string(10) "http://a.b"

So basically wherever you notice false as return value that is an INVALID URL for you.

anubhava
  • 761,203
  • 64
  • 569
  • 643