php-ip icon indicating copy to clipboard operation
php-ip copied to clipboard

Help| Unique continer or special IP's from list

Open z3ro opened this issue 1 year ago • 0 comments

Hello, i have a list of IP's i want to pull out only the unique IP's (blocks) from the list. For example, 192.168.0.0/32 is in 192.168.0.0/24 so it should just print the container 192.168.0.0/24 and the rest containers like ( 192.148.0.1/8 which include 192.148.0.1/9 ) and the rest of unique blocks.

My code like this, i was straggling with no correct results. thanks for

` $array1 = array('192.168.0.0/32', '192.148.0.1/8', '142.158.2.1/23', '192.148.0.1/9', '192.168.1.0/24', '192.168.0.0/24'); $res=[]; foreach($array1 as $key=> $blockx){

foreach($array1 as  $key2=> $blocky){
	$block = IPBlock::create($blockx);
	$check = $block->contains($blocky);
	if(!$check){
		if(count($res)==0){
			//echo $blockx .":".$blocky.":".$check."\n";
			//if(!in_array($blocky,$res)){
			$res[]=$blocky;
			//}
		}
		foreach($res as   $blockz){
			
			$block = IPBlock::create($blocky);
			$check = $block->contains($blockz);
			if(!$check){
			foreach($res as $keyxx =>  $blockz){
				$block = IPBlock::create($blockz);
				$check = $block->contains($blocky);
				if(!$check){
					if(!in_array($blocky,$res)){
				$res[]=$blocky;
				}else{
					unset($res[$keyxx]);
					if(!in_array($blocky,$res)){
					$res[]=$blocky;
					}
				}
				}
			}
				
			}else{
				foreach($res as  $keyz=> $blockz){
				$block = IPBlock::create($blockz);
				$check = $block->contains($blocky);
				if(!$check){
					if(!in_array($blocky,$res)){
				$res[]=$blocky;
				}else{
					unset($res[$keyz]);
					if(!in_array($blocky,$res)){
					$res[]=$blocky;
					}
				}
				}
			}
			}
		}
	}

}

} print_r($res);`

z3ro avatar Dec 11 '23 14:12 z3ro