note 23544 added to function.ip2long

From: Date: Sun, 21 Jul 2002 20:59:01 +0000
Subject: note 23544 added to function.ip2long
Groups: php.notes 
Request: Send a blank email to php-notes+get-33368@lists.php.net to get a copy of this message
Irritating that dechex and hexdec act like signed 32 bit integers for <4.1.0 The below would be a bit easier to handle without that limitation.... Would also be nice to have an option to prepend zeros in dechex since like binary it's representation independant, ie, in both string and numbers you can pretty much treat them the same, but you do need the full 2 (or four etc) string positions if you want to keep that property. function IPtoInt($ipstring) { $a = explode(".", $ipstring); for ($i=0; $i<3; $i++) { if($a[$i] < 16) { $hex .= "0" . DecHex($a[$i]); } else { $hex .= DecHex($a[$i]); } } // made the hex, now to decimal and times 100 hex (=256, or shift left 8 bits), then add the last bit $dec = (HexDec($hex) << 8 ) + $a[3]; return $dec; } Without the limits : function IPtoInt($ipstring) { $a = explode(".", $ipstring); for ($i=0; $i<4; $i++) { // always show 2 characters, ie 01 - 0f $hex .= DecHex($a[$i],2); } return HexDec($hex); } -- http://www.php.net/manual/en/function.ip2long.php http://master.php.net/manage/user-notes.php?action=edit+23544 http://master.php.net/manage/user-notes.php?action=delete+23544 http://master.php.net/manage/user-notes.php?action=reject+23544

« previous php.notes (#33368) next »