数据文件ip.txt,其中有部份IP数据相同,如:
1348453617|95.6.5.162|2012-09-24 10:26:57
1348453024|180.254.252.144|2012-09-24 10:17:04
1348453020|180.254.252.144|2012-09-24 10:17:00
1348453018|180.254.252.144|2012-09-24 10:16:58
1348453008|112.210.114.169|2012-09-24 10:16:48
1348453007|112.210.114.169|2012-09-24 10:16:47
1348372772|119.178.17.181|2012-09-23 11:59:32
1348363141|110.187.62.65|2012-09-23 09:19:01
怎么写一个php文件,只显示输出有重复IP数据的内容,如:
1348453024|180.254.252.144|2012-09-24 10:17:04
1348453020|180.254.252.144|2012-09-24 10:17:00
1348453018|180.254.252.144|2012-09-24 10:16:58
1348453008|112.210.114.169|2012-09-24 10:16:48
1348453007|112.210.114.169|2012-09-24 10:16:47
再怎么执行php,删除重复数据只保留重复数据中的最后一条,得到:
1348453617|95.6.5.162|2012-09-24 10:26:57
1348453024|180.254.252.144|2012-09-24 10:17:04
1348453008|112.210.114.169|2012-09-24 10:16:48
1348372772|119.178.17.181|2012-09-23 11:59:32
1348363141|110.187.62.65|2012-09-23 09:19:01
<?
//请高手帮写一下php文件得到以上效果
$ipfile="ip.txt";
if($del==1){
$old=file("$ipfile");
$num=count($old);
$fp=fopen("$ipfile","w");
$no=$num-1;
for ($i=0;$i<$num;$i++){
list($usre,$ip,$time)=explode("|",$old[$i]);
if($ip!=$delip){ fputs($fp,$old[$i]); }
}
fclose($fp);
}
//ip.php?del=1&delip=$ip这样写会删除所有重复IP,怎么弄才能删除重复IP并保留最后一条重复的ip
?>
------解决方案--------------------
for ($i=0;$i<$num;$i++){
list($usre,$ip,$time)=explode("|",$old[$i]);
if($ip!=$delip){ fputs($fp,$old[$i]); }
}
改为
$t = array();
for ($i=0;$i<$num;$i++){
list($usre,$ip,$time)=explode("|",$old[$i]);
if(isset($t[$ip])) {
unset($old[$t[$ip]]);
unset($t[$ip]);
}
$t[$ip] = $i;
}
fputs($fp, join('', $old));
------解决方案--------------------
<?php
$fp=fopen("ip.txt","r");
while($file=fgets($fp)){
$arr[]=trim($file);
}
for($i=0;$i<count($arr);$i++){
$t=explode("|",$arr[$i]);
$t1[]=array("list"=>$t[0],"ip"=>$t[1],"time"=>$t[1]);
}
for($j=0;$j<count($t1);$j++){
if($t1[$j]['ip']==$t1[$j+1]['ip']){
}else{
$t2[]=$t1[$j];
}
}
fclose($fp);
$fp2=fopen("ip.txt","w");
foreach($t2 as $val){
$t3=$val['list']."|".$val['ip']."|".$val['time'];
fwrite($fp2,$t3."\r\n");
}
fclose($fp2);
?>
测试了,没问题