View Full Version : convert IP to 10 digit decimal number
Aurous
03-10-2005, 10:04 AM
Hi guys I need help in converting IP in php.
for example, if we have this IP: 123.456.789.012
I would like to make it as 012.789.456.123
and then convert it to a 10 digit decimal number. This is for a hack I am working on and will be greatful if anyone can help.
Thanks
Aurous
Marco van Herwaarden
03-10-2005, 10:12 AM
Why 10 digits, why not 12?
Aurous
03-10-2005, 01:04 PM
Why 10 digits, why not 12?
http://livinginternet.com/i/iw_dns_alias.htm
filburt1
03-10-2005, 01:07 PM
<a href="http://us4.php.net/manual/en/function.ip2long.php" target="_blank">http://us4.php.net/manual/en/function.ip2long.php</a>
Aurous
03-10-2005, 01:08 PM
http://us4.php.net/manual/en/function.ip2long.php
ya I understood the ip2long function. How about reversing the IP address before I use the function?
Marco van Herwaarden
03-10-2005, 02:12 PM
How about:
$reversedlong = ip2long(implode(".",array_reverse(explode(".", $ip))));
filburt1
03-10-2005, 02:47 PM
That's what I was going to suggest as well.
Marco van Herwaarden
03-10-2005, 02:49 PM
Yes i know you suggested it ;) but he had trouble reversing it.
Aurous
03-10-2005, 03:02 PM
Yes i know you suggested it ;) but he had trouble reversing it.
Actually the way I need to reverse is as follows:
If the IP is suppose: 123.456.78.90
It should reverse it to 90.78.456.123
They way you suggest would make it 321.654.87.09 (i think), which I dont want to do.
basically if the IP I need to get this -> 3092948441 when this is entered as IP -> 217.165.90.184
This is an example.
Marco van Herwaarden
03-10-2005, 03:55 PM
Don't think too much ;)
It would already do that, i a review with the ip you now provided i found out that the sprintf is really neccesary to avoid negative numbers.
<?php
$ip = "217.165.90.184";
echo "<br />ip: $ip";
$reversed = implode(".",array_reverse(explode(".", $ip)));
echo "<br />reversed: $reversed";
$reversedlong = sprintf("%u",ip2long(implode(".",array_reverse(explode(".", $ip)))));
echo "<br />reversedlong: $reversedlong";
?>
Will produce:
ip: 217.165.90.184
reversed: 184.90.165.217
reversedlong: 3092948441
Paul M
03-10-2005, 04:10 PM
Why do you need to reverse them ?
Aurous
03-10-2005, 08:33 PM
Don't think too much ;)
It would already do that, i a review with the ip you now provided i found out that the sprintf is really neccesary to avoid negative numbers.
<?php
$ip = "217.165.90.184";
echo "<br />ip: $ip";
$reversed = implode(".",array_reverse(explode(".", $ip)));
echo "<br />reversed: $reversed";
$reversedlong = sprintf("%u",ip2long(implode(".",array_reverse(explode(".", $ip)))));
echo "<br />reversedlong: $reversedlong";
?>
Will produce:
ip: 217.165.90.184
reversed: 184.90.165.217
reversedlong: 3092948441
Excellent MarcoH64 .. that did help! You are right about sprintf, thanks a million!
Paul M - Just have to, thats how things work sometimes :p
BTW, thanks for the help guys, i got what i wanted :) Really appreciate everyones help here, especially MarcoH64.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.