加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

从字符串php中提取顶级域名

发布时间:2020-12-13 13:29:39 所属栏目:PHP教程 来源:网络整理
导读:我需要从字符串中提取域名,这可能是任何东西.如: $sitelink="http://www.somewebsite.com/product/3749875/info/overview.html"; 要么 $sitelink="http://subdomain.somewebsite.com/blah/blah/whatever.php"; 在任何情况下,我都想提
我需要从字符串中提取域名,这可能是任何东西.如:
$sitelink="http://www.somewebsite.com/product/3749875/info/overview.html";

要么

$sitelink="http://subdomain.somewebsite.com/blah/blah/whatever.php";

在任何情况下,我都想提取’somewebsite.com’部分(可能是任何东西),并丢弃其余部分.

随着 parse_url($url)
<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));
?>

The above example will output:

Array
(
    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor
)

使用thos值

echo parse_url($url,PHP_URL_HOST); //hostname

要么

$url_info = parse_url($url);
echo $url_info['host'];//hostname

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读