Net_IPv6::compress() – compress an IPv6 address


require_once 'Net/IPv6.php';

string Net_IPv6::compress ( string $ip , boolean $force = false )


Compresses an IPv6 address. RFC 2373 allows you to compress zeros in an address to '::'. This function expects an valid IPv6 address and compresses successive zeros to '::'

FF01:0:0:0:0:0:0:101 -> FF01::101
      0:0:0:0:0:0:0:1      -> ::1

By default, the method does not touch an already compressed address and returns the address as is. This happens also in case of an address that is compressed but can be compressed further, for example an address like FF01::0:1 will be returned as is.

Since version 1.2.1, you can change this behavoir to enforce compression by setting $force to true.


  • string $ip - the IP address to compress

  • boolean $force - if true, an already compressed IP address will be compressed again

Return value

string - the compressed IP is an IPv6 address


This function can be called statically.