check if a string is a URL [duplicate]

2019-01-23 15:12发布

问题:

This question already has an answer here:

  • Best way to check if a URL is valid 8 answers

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?

回答1:

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...



回答2:

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....



回答3:

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.



回答4:

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



回答5:

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.



回答6:

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.



回答7:

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]|\/)))";


回答8:

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.



标签: php regex url