Skip to content

Instantly share code, notes, and snippets.

@mgng mgng/ipv6full.php
Created Nov 13, 2012

Embed
What would you like to do?
圧縮されたIPv6アドレスを展開するやつ
<?php
function kuso( $ip_addr, $strtoupper = false ) {
$in_addr = @inet_pton( $ip_addr );
if ( $in_addr === false || strlen( $in_addr ) !== 16 ) {
return false;
}
$unpack = unpack( 'C*', $in_addr );
$format = $strtoupper ? "%02X%02X" : "%02x%02x";
$ip_full = array();
for( $i=1, $len=count($unpack); $i<$len; $i=$i+2 ) {
$ip_full[] = sprintf( $format, $unpack[$i], $unpack[$i+1] );
}
return implode( ':', $ip_full );
}
var_dump(
kuso( '' ),
kuso( 'aaa' ),
kuso( '127.0.0.1' ),
kuso( '::1' ),
kuso( '1::' ),
kuso( '2001:db8::9abc' ),
kuso( '2001:db8::9abc', true )
);
bool(false)
bool(false)
bool(false)
string(39) "0000:0000:0000:0000:0000:0000:0000:0001"
string(39) "0001:0000:0000:0000:0000:0000:0000:0000"
string(39) "2001:0db8:0000:0000:0000:0000:0000:9abc"
string(39) "2001:0DB8:0000:0000:0000:0000:0000:9ABC"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.