php - How to get the website name based on whether the website supports http or https?
I have links of the following shape:http://website.com/stuff
andhttps://website.nl/stuff
. I would like to return the http/https part with the website name and domain. So for example, if I have
https://www.stackoverflow.com/questions/55823298/how-do-i-check-if-a-string-is-entirely-made-of-the-same-substring
I would like to returnhttps://www.stackoverflow.com
.
if I have
http://www.website.nl/questions/55823298/how-do-i-check-if-a-string-is-entirely-made-of-the-same-substring
I would like to returnhttp://www.website.nl
I now have this very basic code to achieve this:
<?php
$urlData = parse_url('https://www.stackoverflow.com/questions/55823298/how-do-i-check-if-a-string-is-entirely-made-of-the-same-substring');
echo "https://".$urlData['host'];
However, I would like that the code looks at the url and decide which prefix it should be,http
orhttps
. How can I achieve that?
Answer
Solution:
I adjusted my code based on the advice from @RiggsFolly as follows and achieved what I wanted:
which returns
http://website.nl
as wished.Answer
Solution:
Get the first five characters of the input string and then compare it if it's "https" or http:".
Quoting linked question: