我有以下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;
该回波的times.hello.com.uk
由四部分组成
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...
相呼应,最后我会得到三个点。
我该如何解决这个问题?
我想去掉末尾的双点和三点。
使用PHP的trim
和implode
。
正如你所说,你想删除字符串末尾的双点和三点:
$domain = implode('.',$parts);
$domain = trim($domain, '.');
print $domain;
看看phps的implode()
函数。
这允许做这样的事情:
echo implode('.', $parts);
PHP有一个implode
方法:
$domain = implode('.',$parts);
你可以在内爆前移除第一件零件:
array_shift($parts);