-1

I have links of the following shape: http://website.com/stuff and https://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 return https://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 return http://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 or https. How can I achieve that?

2 Answers2

1

I adjusted my code based on the advice from @RiggsFolly as follows and achieved what I wanted:

<?php
$urlData = parse_url('http://website.nl/questions/55823298/how-do-i-check-if-a-string-is-entirely-made-of-the-same-substring');
echo $urlData['scheme'].'://'.$urlData['host'];

which returns http://website.nl as wished.

-3

Get the first five characters of the input string and then compare it if it's "https" or http:".

Quoting linked question:

For single-byte strings (e.g. US-ASCII, ISO 8859 family, etc.) use substr and for multi-byte strings (e.g. UTF-8, UTF-16, etc.) use mb_substr:

    // singlebyte strings
    $result = substr($myStr, 0, 5);
    // multibyte strings
    $result = mb_substr($myStr, 0, 5);
Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
trippeljojo
  • 65
  • 1
  • 11
  • 1
    This does not actually answer the question. The question is not asking how to get a fixed number of characters from a string. It is asking how to get the first portion of the URL, including the domain name, but not the path. That is inherently not a fixed number of characters. If you are wanting to use `substr` for this purpose, then you need to also have a way of determining the number of characters you need. – Makyen May 19 '19 at 17:48
  • 1
    @Makyen Hello thanks for feedback, noted, I will review more carefully. – Tatranskymedved May 19 '19 at 18:17
  • @Makyen You're right, the question of the title is not answered by this. However, the final question in the text ("However, I would like that the code looks at the url and decide which prefix it should be, http or https. How can I achieve that?") is actually answered. Nevertheless, using `$urlData['scheme']` as proposed by aardappel is the better option. – trippeljojo May 20 '19 at 06:17
  • 1
    @trippeljojo Actually, no, even that part is not answered. This just shows how to get the the first few characters of the string. It doesn't show comparing it to anything. At best, this takes one step along the path that's needed to implement the functionality that is very nebulously defined by that statement. – Makyen May 20 '19 at 06:26