Home >php教程 >php手册 >PHP实现截取一个URL中的主域名

PHP实现截取一个URL中的主域名

WBOY
WBOYOriginal
2016-06-21 08:52:411904browse

PHP实例教程,如何截取一个URL中的主域名,如下代码:
$S = 'http://bbs.webjx.com';
$S = parse_url($S);
$S = strtolower($S['host']) ; //取域名部分
//echo $S;

$domain = array('com','cn','name','org','net'); //域名后缀 有新的就扩展这吧
$SS = $S;
$dd = implode('',$domain);
$SS = preg_replace('/(\.('.$dd.'))*\.('.$dd.')$/iU','',$SS); 把后面的域名后缀部分去掉
 
$SS = explode('.',$SS);
$SS = array_pop($SS);  //取最后的主域名
$SS = substr($S,strrpos($S,$SS));  //加上后缀拼成完成的主域名
echo $SS;
?>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn