php url parse string,urlparse-如何从php中的URL剥离域名?

urlparse-如何从php中的URL剥离域名?

我正在寻找一种方法(或函数)来去除馈入该函数的任何URL的domain.ext部分。 域扩展名可以是任何内容(.com,.co.uk,.nl,.whatever),输入的URL可以是[http://www.domain.com]到www.domain.com。 /path/script.php?=无论如何

最好的方法是什么?

qasimzee asked 2020-07-05T23:19:26Z

7个解决方案

97 votes

parse_url将URL转换为关联数组:

php > $foo = "http://www.example.com/foo/bar?hat=bowler&accessory=cane";

php > $blah = parse_url($foo);

php > print_r($blah);

Array

(

[scheme] => http

[host] => www.example.com

[path] => /foo/bar

[query] => hat=bowler&accessory=cane

)

Robert Elwell answered 2020-07-05T23:19:39Z

12 votes

您可以使用parse_url()执行此操作:

$url = 'http://www.example.com';

$domain = parse_url($url, PHP_URL_HOST);

$domain = str_replace('www.','',$domain);

在此示例中,$ domain应该包含example.com,无论其是否带有www。 它还适用于.co.uk之类的域

DavidM answered 2020-07-05T23:20:04Z

10 votes

您还可以编写一个正则表达式以获取所需的内容。

这是我的尝试:

$pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';

$url = 'http://www.example.com/foo/bar?hat=bowler&accessory=cane';

if (preg_match($pattern, $url, $matches) === 1) {

echo $matches[0];

}

输出为:

example.com

该模式还考虑了“ example.com.au”等域名。

注意:我尚未查阅相关的RFC。

firstresponder answered 2020-07-05T23:20:41Z

2 votes

这是几个简单的函数,可从普通或长域(test.sub.domain.com)或url([http://www.example.com)”获得根域(example.com)。

/**

* Get root domain from full domain

* @param string $domain

*/

public function getRootDomain($domain)

{

$domain = explode('.', $domain);

$tld = array_pop($domain);

$name = array_pop($domain);

$domain = "$name.$tld";

return $domain;

}

/**

* Get domain name from url

* @param string $url

*/

public function getDomainFromUrl($url)

{

$domain = parse_url($url, PHP_URL_HOST);

$domain = $this->getRootDomain($domain);

return $domain;

}

Mark Shust answered 2020-07-05T23:21:01Z

1 votes

解决了这个...

假设我们正在呼叫dev.mysite.com,并且要提取“ mysite.com”

$requestedServerName = $_SERVER['SERVER_NAME']; // = dev.mysite.com

$thisSite = explode('.', $requestedServerName); // site name now an array

array_shift($thisSite); //chop off the first array entry eg 'dev'

$thisSite = join('.', $thisSite); //join it back together with dots ;)

echo $thisSite; //outputs 'mysite.com'

也可以与mysite.co.uk一起使用,因此应该在任何地方都可以使用:)

z3ro answered 2020-07-05T23:21:30Z

0 votes

我花了一些时间考虑使用正则表达式是否有意义,但最后我认为没有意义。

firstresponder的regexp几乎说服了我,这是最好的方法,但是它在缺少斜杠的情况下不起作用(例如[http://example.com,])。 我使用以下命令修复了该问题:parse_url(),但后来我意识到,对于“ [http://example.com/index.htm””这样的网址,它匹配了两次。 那样还不错(只是使用第一个),但是它在类似这样的内容上也匹配了两次:“ [http://abc.ed.fg.hij.kl.mn/”,]和第一个匹配项 是不正确的。 :(

一位同事建议仅获取主机(通过parse_url()),然后获取最后的两个或三个数组位('。'上的split())。这两个或三个将基于域列表,例如'co.uk ',等等。组成该列表变得很困难。

livingtech answered 2020-07-05T23:22:00Z

0 votes

提取域部分的唯一正确方法是使用公共后缀列表(TLD的数据库)。 我推荐TLDExtract软件包,这是示例代码:

$extract = new LayerShifter\TLDExtract\Extract();

$result = $extract->parse('www.domain.com/path/script.php?=whatever');

$result->getSubdomain(); // will return (string) 'www'

$result->getHostname(); // will return (string) 'domain'

$result->getSuffix(); // will return (string) 'com'

Oleksandr Fediashov answered 2020-07-05T23:22:20Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值