Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matchilling/2464558eae46c19a3d364c43414cd514 to your computer and use it in GitHub Desktop.
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
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