note 23544 deleted from function.ip2long by sniper

From: Date: Sat, 20 Sep 2003 07:50:00 +0000
Subject: note 23544 deleted from function.ip2long by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-56799@lists.php.net to get a copy of this message
Note Submitter: marcth at xs4all dot nl ---- 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); }

« previous php.notes (#56799) next »