Jump to content

I have a logic issue and I just can't figure it out ... pls help!


phporcaffeine

Recommended Posts

Okay,

 

I have a list of various ip address that are not in any particular order or form any particular subnet ... think of them as just random addresses.  Here is a sample:

 

193.122.140.196

193.122.140.192

212.209.042.128

193.132.159.000

203.166.028.064

156.070.222.001

218.104.051.160

194.034.064.001

194.034.065.001

194.034.066.001

194.034.067.001

199.068.016.000

067.111.075.099

212.044.025.206

212.044.008.034

195.102.024.074

193.122.140.207

212.209.042.135

193.132.159.255

203.166.028.127

156.070.222.255

218.104.051.174

 

Now I need to iterate over this list "somehow" to identify and group together, like addresses so I can express them by ranges.  For example:

 

193.122.140.207 and 193.122.159.255 would be expressed as 193.122.*.*

193.122.140.20 and 193.122.140.80 would be expressed as 193.122.140.20-80

 

I don't seem to be able to figure out how I would logically do it?  Any help or ideas?

 

Thanks in advance

Link to comment
Share on other sites

Yeah, you definitely need to provide more details. If you were to combine the two examples you posted what would the result be:

 

- 193.122.140.207

- 193.122.140.20

- 193.122.140.80

- 193.122.159.255

 

Would it be

- 193.122.140.20-207

- 193.122.159.255

 

Or just:

- 193.122.*.*

 

I would start by putting all of the IPs into multiple-dimensional arrays with each octet as a separate array element. Then sort by each octet. You would them have them in a logincal order to process - once you have clear rules for processing them.

Link to comment
Share on other sites

If I understood.....

 

<?php

function FormatIP($IP) {

  if(preg_match('/[0-9]+\.[0-9]+\.[0-9]+\.[0-9][0-9][0-9]/',$IP)) {
    $ip = preg_replace('/([0-9]+\.)([0-9]+\.)([0-9]+\.)([0-9]+)/','\\1\\2*.*',$IP);
  }
    else
    {
      $ip = str_replace(' ','',preg_replace('/([0-9]+\.)([0-9]+\.)([0-9]+\.)([0-9]+)/','\\1\\2\\3 20-80',$IP)); 
    }

  return $ip;
}

//$ip = '192.168.0.100';  <-- This will return 192.168.*.*
//$ip = '192.168.0.10'; <-- This will return 192.168.0.20-80

$p =  FormatIP($ip);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.