如果 URL 中不存在,如何添加 http:// [英] How to add http:// if it doesn't exist in the URL
本文介绍了如果 URL 中不存在,如何添加 http://的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将 http://
添加到尚未包含协议的 URL(例如 http://
、https:///
或 ftp://
)?
How can I add http://
to a URL if it doesn't already include a protocol (e.g. http://
, https://
or ftp://
)?
示例:
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
推荐答案
@nickf 代码的修改版本:
A modified version of @nickf code:
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
识别 ftp://
、ftps://
、http://
和 https://
> 不区分大小写.
Recognizes ftp://
, ftps://
, http://
and https://
in a case insensitive way.
这篇关于如果 URL 中不存在,如何添加 http://的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文