如果网址中不存在http://,如何添加? [英] How to add http:// if it doesn't exist in the URL?

查看:115
本文介绍了如果网址中不存在http://,如何添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果http://不包含协议(例如http://https://ftp://),如何将其添加到URL?

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.

这篇关于如果网址中不存在http://,如何添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆