Created
November 13, 2012 07:54
-
-
Save mgng/4064566 to your computer and use it in GitHub Desktop.
圧縮されたIPv6アドレスを展開するやつ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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