public function formatRange ($range)
	{
		
		// CIDR
		
		if (strpos ($range, '/') !== FALSE)
		{
			
			// Get the IP and netmask
			
			list ($ip, $netmask) = explode ('/', $range, 2);
			
			// Split decimal and pad to ensure we have 4 octets

			$ip	= implode ('.', array_pad (explode ('.', $ip), 4, 0));
			
			// If the netmask is a dotted decimal format
			
			if (strpos ($netmask, '.') !== FALSE)
			{
				
				// Convert wildcards
				
				$netmask		= str_replace ('*', '0', $netmask);
				
				// Convert to long
				
				$ipLong			= ip2long ($ip);
				$netmaskLong	= ip2long ($netmask);
				
				// Check IPs are valid
				
				if ($ipLong === FALSE || $netmaskLong === FALSE)
				{
					return FALSE;
				}
				
				// Return range
				
				return array (
					$ip . '/' . $netmask,
					array (
						(float)sprintf ("%u", $ipLong),
						(float)sprintf ("%u", $netmaskLong)
					)
				);
				
			}
			
			// Else netmask is a CIDR block
			
			else
			{
				
				// Convert netmask block to bit-mask
				// Bitwise NOT on wildcard
				
				
				$wildcard	= pow (2, (32 - (int)$netmask)) - 1;
				$netmaskDec	= ~$wildcard;
				
				// Return range
				
				return array (
					$ip . '/' . $netmask,
					array (
						(float)sprintf ("%u", ip2long ($ip)),
						$netmaskDec
					)
				);
				
			}
			
			// Return TRUE
			
			return TRUE;
			
		}
		
		// Wildcard or Start-End
		
		elseif (strpos ($range, '*') !== FALSE || strpos ($range, '-') !== FALSE)
		{
			
			// Wildcard
			
			if (strpos ($range, '*') !== FALSE)
			{
				$lower	= str_replace ('*', '0', $range);
				$upper	= str_replace ('*', '255', $range);
				$name	= $lower . '-' . $upper;
			}
			
			// Start-End
			
			else
			{
				list ($lower, $upper)	= explode ('-', $range, 2);
				$name	= $range;
			}
			
			// Convert to long

			$lowerLong	= ip2long ($lower);
			$upperLong	= ip2long ($upper);

			// Check IPs are valid

			if ($lowerLong === FALSE || $upperLong === FALSE)
			{
				return FALSE;
			}
			
			// Return range

			return array (
				$name,
				array (
					(float)sprintf ("%u", $lowerLong),
					(float)sprintf ("%u", $upperLong)
				)
			);
			
		}
		
		// Else a single IP

		if (strpos ($range, '.') !== FALSE)
		{
			
			// Return range

			return array (
				$range,
				(float)sprintf ("%u", ip2long ($range))
			);
			
		}
		
		// No range to return
		
		return FALSE;
		
	}
public function formatRange ($range)
	{
		
		// CIDR
		
		if (strpos ($range, '/') !== FALSE)
		{
			
			// Get the IP and netmask
			
			list ($ip, $netmask) = explode ('/', $range, 2);
			
			// Split decimal and pad to ensure we have 4 octets

			$ip	= implode ('.', array_pad (explode ('.', $ip), 4, 0));
			
			// If the netmask is a dotted decimal format
			
			if (strpos ($netmask, '.') !== FALSE)
			{
				
				// Convert wildcards
				
				$netmask		= str_replace ('*', '0', $netmask);
				
				// Convert to long
				
				$ipLong			= ip2long ($ip);
				$netmaskLong	= ip2long ($netmask);
				
				// Check IPs are valid
				
				if ($ipLong === FALSE || $netmaskLong === FALSE)
				{
					return FALSE;
				}
				
				// Return range
				
				return array (
					$ip . '/' . $netmask,
					array (
						(float)sprintf ("%u", $ipLong),
						(float)sprintf ("%u", $netmaskLong)
					)
				);
				
			}
			
			// Else netmask is a CIDR block
			
			else
			{
				
				// Convert netmask block to bit-mask
				// Bitwise NOT on wildcard
				
				
				$wildcard	= pow (2, (32 - (int)$netmask)) - 1;
				$netmaskDec	= ~$wildcard;
				
				// Return range
				
				return array (
					$ip . '/' . $netmask,
					array (
						(float)sprintf ("%u", ip2long ($ip)),
						$netmaskDec
					)
				);
				
			}
			
			// Return TRUE
			
			return TRUE;
			
		}
		
		// Wildcard or Start-End
		
		elseif (strpos ($range, '*') !== FALSE || strpos ($range, '-') !== FALSE)
		{
			
			// Wildcard
			
			if (strpos ($range, '*') !== FALSE)
			{
				$lower	= str_replace ('*', '0', $range);
				$upper	= str_replace ('*', '255', $range);
				$name	= $lower . '-' . $upper;
			}
			
			// Start-End
			
			else
			{
				list ($lower, $upper)	= explode ('-', $range, 2);
				$name	= $range;
			}
			
			// Convert to long

			$lowerLong	= ip2long ($lower);
			$upperLong	= ip2long ($upper);

			// Check IPs are valid

			if ($lowerLong === FALSE || $upperLong === FALSE)
			{
				return FALSE;
			}
			
			// Return range

			return array (
				$name,
				array (
					(float)sprintf ("%u", $lowerLong),
					(float)sprintf ("%u", $upperLong)
				)
			);
			
		}
		
		// Else a single IP

		if (strpos ($range, '.') !== FALSE)
		{
			
			// Return range

			return array (
				$range,
				(float)sprintf ("%u", ip2long ($range))
			);
			
		}
		
		// No range to return
		
		return FALSE;
		
	}
public function formatRange ( $ range ) { // CIDR if ( strpos ( $ range , '/' ) !== FALSE ) { // Get the IP and netmask list ( $ ip , $ netmask ) = explode ( '/' , $ range , 2 ) ; // Split decimal and pad to ensure we have 4 octets $ ip = implode ( '.' , array_pad ( explode ( '.' , $ ip ) , 4 , 0 ) ) ; // If the netmask is a dotted decimal format if ( strpos ( $ netmask , '.' ) !== FALSE ) { // Convert wildcards $ netmask = str_replace ( '*' , '0' , $ netmask ) ; // Convert to long $ ipLong = ip2long ( $ ip ) ; $ netmaskLong = ip2long ( $ netmask ) ; // Check IPs are valid if ( $ ipLong === FALSE || $ netmaskLong === FALSE ) { return FALSE ; } // Return range return array ( $ ip . '/' . $ netmask , array ( ( float ) sprintf ( "%u" , $ ipLong ) , ( float ) sprintf ( "%u" , $ netmaskLong ) ) ) ; } // Else netmask is a CIDR block else { // Convert netmask block to bit-mask // Bitwise NOT on wildcard $ wildcard = pow ( 2 , ( 32 - ( int ) $ netmask ) ) - 1 ; $ netmaskDec = ~ $ wildcard ; // Return range return array ( $ ip . '/' . $ netmask , array ( ( float ) sprintf ( "%u" , ip2long ( $ ip ) ) , $ netmaskDec ) ) ; } // Return TRUE return TRUE ; } // Wildcard or Start-End elseif ( strpos ( $ range , '*' ) !== FALSE || strpos ( $ range , '-' ) !== FALSE ) { // Wildcard if ( strpos ( $ range , '*' ) !== FALSE ) { $ lower = str_replace ( '*' , '0' , $ range ) ; $ upper = str_replace ( '*' , '255' , $ range ) ; $ name = $ lower . '-' . $ upper ; } // Start-End else { list ( $ lower , $ upper ) = explode ( '-' , $ range , 2 ) ; $ name = $ range ; } // Convert to long $ lowerLong = ip2long ( $ lower ) ; $ upperLong = ip2long ( $ upper ) ; // Check IPs are valid if ( $ lowerLong === FALSE || $ upperLong === FALSE ) { return FALSE ; } // Return range return array ( $ name , array ( ( float ) sprintf ( "%u" , $ lowerLong ) , ( float ) sprintf ( "%u" , $ upperLong ) ) ) ; } // Else a single IP if ( strpos ( $ range , '.' ) !== FALSE ) { // Return range return array ( $ range , ( float ) sprintf ( "%u" , ip2long ( $ range ) ) ) ; } // No range to return return FALSE ; }
Format an IP address range

Valid IP ranges can be:

Single:	  192.168.1.1
CIDR:      192.168.1/24, 192.168.1.0/24 or 192.168.1.0/255.255.255.0
Wildcard:  192.168.1.*
Start-End: 192.168.1.0-192.168.1.255

@param string $range Valid IP range
@return FALSE|array (name, range)
Format an IP address range