【发布时间】:2014-12-08 01:01:49
【问题描述】:
我有以下 PHP 代码
$businessWebsite = 'http://www.times.hello.com.uk/articles/view/20.141013/local/police-killer.gets-10-more-months-over-drugs.539550';
$host = parse_url($businessWebsite, PHP_URL_HOST );
$parts = explode( '.', $host);
//$parts = array_reverse( $parts );
$domain = $parts[1].'.'.$parts[2].'.'.$parts[3].'.'.$parts[4];
print_r($parts);
echo $domain;
这个echo的times.hello.com.ukthis由四部分组成
Array
(
[0] => www
[1] => times
[2] => hello
[3] => com
[4] => uk
)
假设我的域是$businessWebsite = 'http://www.times.com.uk/articles/view/20.141013/local/police-killer.gets-10-more-months-over-drugs.539550';
它会回显times.hello.com..,最后我会得到两个点。
如果域是$businessWebsite = 'http://www.times.com/articles/view/20.141013/local/police-killer.gets-10-more-months-over-drugs.539550';
它会回显times.com...我最后会得到三个点。
我该如何解决这个问题?
我想去掉末尾的双点和三点。
【问题讨论】:
-
你有什么问题?
-
我想去掉末尾的双点和三点。
-
停止假设 $parts[3] 和 $parts[4] 确实存在,并实际检查它们是否存在....如果它们不存在,请不要回显它们或'。 '.... 为什么不使用 array_slice() 来提取要显示的数组部分 (
$parts = str_slice(explode( '.', $host), 1);),然后用 '.' 内爆创建一个字符串,然后您可以回显 -
如果您需要从任何网址获取域名,您可以使用正则表达式:stackoverflow.com/questions/3442333/…
-
@Thiago256 该问题的公认答案建议使用
parse_url。