php 遍历域列表文件并删除子域

php loop through domain list file and remove subdomains

我想列出一个 "file" 有 sub.domain.tld 个名字,删除子并列出 uniqe domain.tld 个名字

我不明白为什么我的输出 ($list) 是最后一行的两倍 "voorbeeld.nl"

<?php
$lines=file("list.txt");

$list = array();  //create empty array
foreach ($lines as $line_num => $line) {
        $line_explode = explode('.', $line);
        array_push($list, $line_explode[1] .'.'. $line_explode[2]);

}
echo "<br>-----------<br>"; 
foreach(array_unique($list) as $Resuld){ 
    echo '>' . $Resuld .'< <br>'; 
}
echo "-----------<br>"; ? >

list.txt:

sub1.example.nl
sub2.example.nl
sub1.example.com
sub2.example.com
sub3.example.com
sub1.voorbeeld.nl
sub2.voorbeeld.nl
sub3.voorbeeld.nl

结果:

-----------
>example.nl < 
>example.com < 
>voorbeeld.nl < 
>voorbeeld.nl< 
-----------

检查 link https://eval.in/686821 这工作正常

在你的输出中,我可以看到一个额外的 space。这很可能是您问题的原因。

>voorbeeld.nl < 
>voorbeeld.nl< 

修改你的代码如下

array_push($list, trim($line_explode[1]) .'.'. trim($line_explode[2]));

你可以使用

$list[$line_explode[1] .'.'. $line_explode[2]]=$line_explode[1] .'.'. $line_explode[2];

代替这个

array_push($list, $line_explode[1] .'.'. $line_explode[2]);