UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

103 lines (89 loc) 3.21 kB
--- layout: page title: "JavaScript ip2long function" comments: true sharing: true footer: true alias: - /functions/view/ip2long:436 - /functions/view/ip2long - /functions/view/436 - /functions/ip2long:436 - /functions/436 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's ip2long {% codeblock network/ip2long.js lang:js https://raw.github.com/kvz/phpjs/master/functions/network/ip2long.js raw on github %} function ip2long (IP) { // From: http://phpjs.org/functions // + 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; } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/network/ip2long.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/network/ip2long.js) ### Example 1 This code {% codeblock lang:js example %} ip2long('192.0.34.166'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 3221234342 {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} ip2long('0.0xABCDEF'); {% endcodeblock %} Should return {% codeblock lang:js returns %} 11259375 {% endcodeblock %} ### Example 3 This code {% codeblock lang:js example %} ip2long('255.255.255.256'); {% endcodeblock %} Should return {% codeblock lang:js returns %} false {% endcodeblock %} ### Other PHP functions in the network extension {% render_partial _includes/custom/network.html %}