fork(1) download
  1. <?php
  2. function normalizeIP( $ip ) {
  3. $ip = inet_pton( $ip );
  4. if( strlen( $ip ) < 5 ) {
  5. $ip = chr( 255 ).chr( 255 ).str_pad( $ip, 4, chr( 0 ), STR_PAD_LEFT );
  6. }
  7. $ip = str_split( str_pad( $ip, 16, chr( 0 ), STR_PAD_LEFT ) );
  8. $out = '';
  9. for( $i = 0; $i < 16; ) {
  10. if( $i && $i % 2 == 0 ) $out .= ':';
  11. $out .= str_pad( dechex( ord( $ip[$i++] ) ), 2, '0', STR_PAD_LEFT );
  12. }
  13. return $out;
  14. }
  15.  
  16. echo normalizeIP( '192.168.0.1' )."\n";
  17. echo normalizeIP( '208.67.222.222' )."\n";
  18. echo normalizeIP( 'dd::caa8:0002' )."\n";
  19. echo normalizeIP( '::ffff:192.168.0.1' )."\n";
  20. ?>
  21.  
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
0000:0000:0000:0000:0000:ffff:c0a8:0001
0000:0000:0000:0000:0000:ffff:d043:dede
00dd:0000:0000:0000:0000:0000:caa8:0002
0000:0000:0000:0000:0000:ffff:c0a8:0001