Php 如何根据网站是否支持http或https获取网站名称?

Php 如何根据网站是否支持http或https获取网站名称?,php,url,Php,Url,我有以下形状的链接:http://website.com/stuff和https://website.nl/stuff。我想返回带有网站名称和域的http/https部分。例如,如果我有 https://www.stackoverflow.com/questions/55823298/how-do-i-check-if-a-string-is-entirely-made-of-the-same-substring 我想返回https://www.stackoverflow.com 如果我有 ht

我有以下形状的链接:
http://website.com/stuff
https://website.nl/stuff
。我想返回带有网站名称和域的http/https部分。例如,如果我有

https://www.stackoverflow.com/questions/55823298/how-do-i-check-if-a-string-is-entirely-made-of-the-same-substring

我想返回
https://www.stackoverflow.com

如果我有

http://www.website.nl/questions/55823298/how-do-i-check-if-a-string-is-entirely-made-of-the-same-substring

我想返回
http://www.website.nl

我现在有一个非常基本的代码来实现这一点:

<?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'];

获取输入字符串的前五个字符,然后比较它是“https”还是“http:”

引用链接:

对于(例如US-ASCII、ISO 8859系列等)使用和对于(例如UTF-8、UTF-16等)使用:


我根据@RiggsFolly的建议调整了代码,如下所示,并实现了我想要的:

<?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'];

这里是指向
echo“thescheme.”$urlData['scheme'];
的链接,如果执行
print\r($urlDate);
您将能够看到完整的数组以及
解析的所有数据
为您提供了细分为多个compartments@RiggsFolly这正是我要找的。谢谢!但请记住,手册中的所有内容都在等待您查看。这实际上并没有回答问题。问题不是如何从字符串中获取固定数量的字符。而是如何获取URL的第一部分,包括域名,但不包括路径。这本身不是一个固定的字符数。如果您想使用
substr
,那么您还需要有一种方法来确定所需的字符数。@Makyen您好,谢谢您的反馈,注意,我会仔细查看。@Makyen你是对的,标题的问题没有用这个来回答。但是,文本中的最后一个问题(“但是,我希望代码查看url并决定它应该是哪个前缀,http或https。我如何实现它?”)实际上得到了回答。尽管如此,使用
$urlData['scheme']
正如Aardapel所建议的那样是更好的选择。@trippeljojo事实上,不,甚至那部分都没有回答。这只是显示了如何获取字符串的前几个字符。它没有显示将它与任何东西进行比较。充其量,这只需要在实现由这项声明。
<?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'];