从 URL 解析域

2022-01-30 00:00:00 php

我需要构建一个从 URL 解析域的函数.

I need to build a function which parses the domain from a URL.

所以,与

http://google.com/dhasjkdas/sadsdds/sdda/sdads.html

http://www.google.com/dhasjkdas/sadsdds/sdda/sdads.html

它应该返回 google.com

http://google.co.uk/dhasjkdas/sadsdds/sdda/sdads.html

它应该返回 google.co.uk.

推荐答案

查看 parse_url():

$url = 'http://google.com/dhasjkdas/sadsdds/sdda/sdads.html';
$parse = parse_url($url);
echo $parse['host']; // prints 'google.com'

parse_url 不能很好地处理严重损坏的 url,但如果您通常期望得到不错的 url,那就没问题了.

parse_url doesn't handle really badly mangled urls very well, but is fine if you generally expect decent urls.

相关文章