php 如果 URL 中不存在,如何添加 http://?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2762061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:36:27  来源:igfitidea点击:

How to add http:// if it doesn't exist in the URL?

phpregex

提问by Ryan

How can I add http://to a URL if it doesn't already include a protocol (e.g. http://, https://or ftp://)?

http://如果 URL 尚未包含协议(例如http://https://ftp://),我该如何添加到 URL 中?

Example:

例子:

addhttp("google.com"); // http://google.com
addhttp("www.google.com"); // http://www.google.com
addhttp("google.com"); // http://google.com
addhttp("ftp://google.com"); // ftp://google.com
addhttp("https://google.com"); // https://google.com
addhttp("http://google.com"); // http://google.com
addhttp("rubbish"); // http://rubbish

回答by Alix Axel

A modified version of @nickf code:

@nickf 代码的修改版本:

function addhttp($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

Recognizes ftp://, ftps://, http://and https://in a case insensitive way.

认识到ftp://ftps://http://https://在不区分大小写的方式。

回答by Ja?ck

At the time of writing, none of the answers used a built-in function for this:

在撰写本文时,没有一个答案为此使用内置函数:

function addScheme($url, $scheme = 'http://')
{
  return parse_url($url, PHP_URL_SCHEME) === null ?
    $scheme . $url : $url;
}

echo addScheme('google.com'); // "http://google.com"
echo addScheme('https://google.com'); // "https://google.com"

See also: parse_url()

也可以看看: parse_url()

回答by Brenton Alker

Simply check if there is a protocol (delineated by "://") and add "http://" if there isn't.

只需检查是否有协议(用“://”表示),如果没有则添加“http://”。

if (false === strpos($url, '://')) {
    $url = 'http://' . $url;
}

Note: This may be a simple and straightforward solution, but Hyman's answer using parse_urlis almost as simple and much more robust. You should probably use that one.

注意:这可能是一个简单而直接的解决方案,但 Hyman 的答案 usingparse_url几乎同样简单且更加健壮。你可能应该使用那个。

回答by ThisDude

The Best Answer for this would be something like this :

最好的答案是这样的:

function addhttp($url) 
{
  return $url = empty(parse_url($url)['scheme']) ? 'http://' . ltrim($url, '/') : $url;
}

回答by Rosdi Kasim

Scan the string for ://, if it does not have it, prepend http://to the string.., everything else just use the string as is.

扫描字符串中的://,如果没有,则http://在字符串前面加上......,其他所有内容都按原样使用字符串。

This will work unless you have rubbish input string.

除非您有垃圾输入字符串,否则这将起作用。

回答by ayan

<?php

if (!preg_match("/^(http|ftp):/", $_POST['url'])) {
   $_POST['url'] = 'http://'.$_POST['url'];
}
$url = $_POST['url'];

?>

this code will add http:// to the URL if it's not there.

如果 URL 不存在,此代码会将 http:// 添加到 URL。

回答by kamasheto

nickf solution modified:

nickf 解决方案修改:

function addhttp($url) {
    if (!preg_match("@^https?://@i", $url) && !preg_match("@^ftps?://@i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

回答by nickf

Try this. Not watertight*, but might be good enough:

尝试这个。不防水*,但可能足够好:

function addhttp($url) {
    if (!preg_match("@^[hf]tt?ps?://@", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}

*: that is, prefixes like "fttps://" are treated as valid.

*:也就是说,像“fttps://”这样的前缀被视为有效。