check if a string is a URL [duplicate]

2019-01-23 15:18发布

This question already has an answer here:

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?

标签: php regex url
8条回答
Melony?
2楼-- · 2019-01-23 15:41

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.

查看更多
干净又极端
3楼-- · 2019-01-23 15:44

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

查看更多
不美不萌又怎样
4楼-- · 2019-01-23 15:46

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

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-23 15:48

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

查看更多
smile是对你的礼貌
6楼-- · 2019-01-23 15:48

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.

查看更多
手持菜刀,她持情操
7楼-- · 2019-01-23 15:50

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.

查看更多
登录 后发表回答