Check whether the URL scheme is HTTP or HTTPS

2019-01-26 16:17发布

问题:

I'm using the following code to add http:// to the URL.

(substr(strtolower($url), 0, 7) == 'http://'?"":"http://").$url

but how can I check whether the original URL contains https? I don't want to use an OR clause.

回答1:

preg_match("@^https?://@", $url)


回答2:

echo parse_url($url, PHP_URL_SCHEME);


回答3:

Use preg_match and a regular expression on your url :

preg_match(^http(s)?://);

If it returns true, then your URL is ok, whether it uses http of https.



回答4:

strncmp($url, 'https:', 6) === 0


标签: php http https