Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether the URL scheme is HTTP or HTTPS

Tags:

http

php

https

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.

like image 722
OHLÁLÁ Avatar asked Sep 07 '11 13:09

OHLÁLÁ


2 Answers

preg_match("@^https?://@", $url)
like image 137
e.dan Avatar answered Oct 11 '22 11:10

e.dan


Answer

echo parse_url($url, PHP_URL_SCHEME);

Reference

docs https://www.php.net/manual/en/function.parse-url.php

parse_url(string $url, int $component = -1): mixed

parse_url function parses a URL and returns an associative array containing any of the various components of the URL that are present. The values of the array elements are not URL decoded.

This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial and invalid URLs are also accepted, parse_url() tries its best to parse them correctly.

like image 26
Fivell Avatar answered Oct 11 '22 10:10

Fivell