Created
May 17, 2017 11:09
-
-
Save matchilling/2464558eae46c19a3d364c43414cd514 to your computer and use it in GitHub Desktop.
Convert an ip address to long integer created by matchilling - https://repl.it/ICl2/1
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
function ipToInteger ($ip) { | |
if (empty($ip)) return 0; | |
$parts = explode('.', $ip); | |
return $parts[3] + $parts[2] * 256 + $parts[1] * 256 * 256 + $parts[0] * 256 * 256 * 256; | |
} | |
$id = '5.148.106.4'; | |
$uk = [35149824, 93968953]; | |
$ipNumber = ipToInteger($id); | |
$isInUnitedKingdom = $ipNumber >= $uk[0] && $ipNumber <= $uk[1]; | |
echo sprintf( | |
'The ip address "%s" translates to ip number "%s" and %s the United Kingdom.', | |
$id, | |
$ipNumber, | |
$isInUnitedKingdom ? 'is mapped to' : 'is not mapped to' | |
) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment