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.
preg_match("@^https?://@", $url)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With