- if ( strpos( $ip, '::' ) !== false ) {
- $ip = str_replace('::', str_repeat(':0', 8 - substr_count($ip, ':')) . ':', $ip);
+ $abbrevPos = strpos( $ip, '::' );
+ if ( $abbrevPos !== false ) {
+ // If the '::' is at the beginning...
+ if( $abbrevPos == 0 ) {
+ $repeat = '0:'; $extra = ''; $pad = 9; // 7+2 (due to '::')
+ // If the '::' is at the end...
+ } else if( $abbrevPos == (strlen($ip)-2) ) {
+ $repeat = ':0'; $extra = ''; $pad = 9; // 7+2 (due to '::')
+ // If the '::' is at the end...
+ } else {
+ $repeat = ':0'; $extra = ':'; $pad = 8; // 6+2 (due to '::')
+ }
+ $ip = str_replace('::', str_repeat($repeat, $pad-substr_count($ip,':')).$extra, $ip);