You are here

function _field_ipaddress_long2shorthand in IP address fields 7

Convert a start address and end address (as longs) into a smaller human readable string.

@example $s=1869573999, $e=1869574110 => 111.111.111.111-222

Parameters

int $start The start address as a long:

int $end The end address as a long:

Return value

string A start and end range as the small formatted string

3 calls to _field_ipaddress_long2shorthand()
field_ipaddress_field_formatter_view in ./field_ipaddress.module
Implements hook_field_formatter_view().
field_ipaddress_field_widget_form in ./field_ipaddress.module
Implements hook_field_widget_form().
_field_ipaddress_long2cidr in ./field_ipaddress.module
Convert a start address and end address (as longs) into a CIDR.

File

./field_ipaddress.module, line 286

Code

function _field_ipaddress_long2shorthand($start, $end) {
  if ($start === $end) {
    $output = long2ip($start);
  }
  else {
    $s = explode('.', long2ip($start));
    $e = explode('.', long2ip($end));
    if ($s[0] === $e[0] && $s[1] === $e[1] && $s[2] === $e[2]) {
      if ($s[3] === '0' && $e[3] === '255') {
        $s[3] = '*';
        $output = implode('.', $s);
      }
      else {
        $s[3] = sprintf('%s-%s', $s[3], $e[3]);
        $output = implode('.', $s);
      }
    }
    else {
      $output = long2ip($start) . ' - ' . long2ip($end);
    }
  }
  return $output;
}