2013-08-06 42 views
0

我得到了兩個文本文件:localhost.txthost.txt從TXT文件中刪除類似的條目?

localhost.txt包含:

127.0.0.1:100 
127.0.0.1:200 
127.0.0.1:300 
127.0.0.1:400 
127.0.0.1:500 
127.0.0.1:600 
127.0.0.1:700 
127.0.0.1:800 
127.0.0.1:(...) 

host.txt包含:

172.39.32.1:100 
172.39.32.1:200 
172.39.32.1:300 
172.39.32.1:400 
172.39.32.1:500 
172.39.32.1:600 
172.39.32.1:700 
172.39.32.1:800 
127.0.0.1:1300 
127.0.0.1:1800 
172.39.32.1:(...) 

(...) 我使用這個腳本

$data = file("localhost.txt"); 
file_put_contents('host.txt', implode(array_unique($data))); 
$fisier = file_get_contents('host.txt'); 

刪除重複。

我需要一個腳本,它也會刪除相似的行,例如如果localhost.txt中的127.0.0.1:200已經存在於host.txt中,那麼腳本將從host.txt中刪除127.0.0.1中的所有行。

任何想法? :) 謝謝

+0

不知道你在問什麼 - 你想刪除該主機名的所有條目,無論端口? – leftclickben

+0

您的第二行將在讀取之前覆蓋'host.txt'。這是故意的嗎? – Jim

+0

@leftclickben如果來自localhost.txt的127.0.0.1.200行已經存在於host.txt中,那麼我需要從host.txt中刪除所有匹配IP的行127:0.0.1 :) – Kris

回答

1
$hosts_lines = file("hosts.txt"); 

foreach($hosts_lines as $line) { 
    $temp = explode(":", $line); 
    $hosts[$temp[0]][] = $temp[1]; 
} 


$localhost = file("localhost.txt"); 

foreach ($localhost as $line) { 
    $temp = explode(":", $line); 
    unset($hosts[$temp[0]]); 
} 


foreach($hosts as $ip => $ports) { 
    foreach ($ports as $port) { 
     printf("%s:%s", $ip, $port); 
    } 
} 
+0

謝謝大家的幫助。我已經購買了現成的腳本:) – Kris