Use PHP functions in JavaScript

JavaScript ip2long

Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
function ip2long (IP) {
    // Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/ip2long    // +   original by: Waldo Malqui Silva
    // +   improved by: Victor
    // +    revised by: fearphage (http://http/my.opera.com/fearphage/)
    // +    revised by: Theriault
    // *     example 1: ip2long('192.0.34.166');    // *     returns 1: 3221234342
    // *     example 2: ip2long('0.0xABCDEF');
    // *     returns 2: 11259375
    // *     example 3: ip2long('255.255.255.256');
    // *     returns 3: false    var i = 0;
    // PHP allows decimal, octal, and hexadecimal IP components.
    // PHP allows between 1 (e.g. 127) to 4 (e.g 127.0.0.1) components.
    IP = IP.match(/^([1-9]\d*|0[0-7]*|0x[\da-f]+)(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?(?:\.([1-9]\d*|0[0-7]*|0x[\da-f]+))?$/i); // Verify IP format.
    if (!IP) {        return false; // Invalid format.
    }
    // Reuse IP variable for component counter.
    IP[0] = 0;
    for (i = 1; i < 5; i += 1) {        IP[0] += !! ((IP[i] || '').length);
        IP[i] = parseInt(IP[i]) || 0;
    }
    // Continue to use IP for overflow values.
    // PHP does not allow any component to overflow.    IP.push(256, 256, 256, 256);
    // Recalculate overflow of last component supplied to make up for missing components.
    IP[4 + IP[0]] *= Math.pow(256, 4 - IP[0]);
    if (IP[1] >= IP[5] || IP[2] >= IP[6] || IP[3] >= IP[7] || IP[4] >= IP[8]) {
        return false;    }
    return IP[1] * (IP[0] === 1 || 16777216) + IP[2] * (IP[0] <= 2 || 65536) + IP[3] * (IP[0] <= 3 || 256) + IP[4] * 1;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
ip2long('192.0.34.166');

Should return

1
3221234342

» Example 2

Running

1
ip2long('0.0xABCDEF');

Should return

1
11259375

Dependencies

No dependencies, you can use this function standalone.

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have ip2long goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Kevin van Zonneveld
4 Sep '09 Permalink

q  @ fearphage: I like it :)
http://github.com/kvz/phpjs/commit/51ac74a14bb0237b202a4a19dc455f45e92da1d5

Gravatar
fearphage
30 Aug '09 Permalink

q  

function ip2long(ip_address) {
  var parts = ip_address.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
  return parts
    ? parts[1] * 16777216 + parts[2] * 65536 + parts[3] * 256 + parts[4] * 1
    : false;
}

Gravatar
Kevin van Zonneveld
7 Jan '09 Permalink

q  @ Victor: That being said: I did take your idea for using fixed numbers, as I don't see any reason why not to, and it will greatly improve the speed of this function. Thx!

Gravatar
Kevin van Zonneveld
7 Jan '09 Permalink

q  @ Victor: For minified versions we have php.min.js &amp; in the near future our compiler will be able to compress a custom, smaller selection of functions for more speed &amp; less size. For the original source however, we like to keep things as readable as possible.

More info: http://trac.plutonia.nl/projects/phpjs/wiki/DeveloperGuidelines#Comments

Thanks a lot for contributing though!

Gravatar
Victor
7 Jan '09 Permalink

q  A minimized version:

[CODE=&quot;Javascript&quot;]
function ip2long(a){if(a.match(/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/)){var b=a.split('.');return(b[0]*16777216)+(b[1]*65536)+(b[2]*256)+(b[3]*1)}else{return&quot;WTF? 0!&quot;}}
[/code]


Contribute a New function

More functions

In this category

inet_ntop
inet_pton
» ip2long
long2ip
setcookie
setrawcookie

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !