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

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

WBOY
Release: 2016-06-21 08:52:41
Original
1911 people have browsed it

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



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template