PHP-每个数组计数的回显数组值
我有以下PHP代码PHP-每个数组计数的回显数组值,php,arrays,Php,Arrays,我有以下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_revers
$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'stimes.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';代码>
它会呼应《时代周刊》…
我会在结尾加上三个点
我如何去解决这个问题
我想删除末尾的双点和三点。看看phps函数
这允许执行以下操作:
echo implode('.', $parts);
PHP有一个内爆方法:
$domain = implode('.',$parts);
可以在内爆之前删除零件的第一项:
array_shift($parts);
使用PHP的trim
和内爆
正如您所说,要删除字符串末尾的两点和三点:
$domain = implode('.',$parts);
$domain = trim($domain, '.');
print $domain;
你有什么问题?我想去掉结尾的两点和三点。不要假设$parts[3]和$parts[4]确实存在,检查它们是否存在。。。。如果他们没有,不要回应他们或“.”。。。。为什么不使用array_slice()来提取要显示的数组部分($parts=str_slice(explode(“.”,$host),1);
),然后使用“.”内爆来创建一个字符串,如果需要从任何URL获取域,则可以使用该字符串进行回显,您可以使用REGEX:@Thiago256该问题的公认答案建议使用parse_url
。对于该问题:if($parts[0]==“www”){unset($parts[0]);}
,然后您可以对其进行内爆和修剪。你能试试吗?太好了!多谢各位@用户2333968谢谢。如果答案正确,请打勾。