Method of IP address mask operation in PHP _php tips

Source: Internet
Author: User
Tags explode

The example of this article describes the approach of PHP for IP address mask operation. Share to everyone for your reference, specific as follows:

IP Resolution:

function Ip_parse ($ip _str) {
 $mark _len =;
 if (Strpos ($ip _str, "/") > 0) {
  list ($ip _str, $mark _len) = Explode ("/", $ip _str);
 }
 $ip = Ip2long ($ip _str);
 $mark = 0xFFFFFFFF << (32-$mark _len) & 0xFFFFFFFF;
 $ip _start = $ip & $mark;
 $ip _end = $ip | (~ $mark) & 0xFFFFFFFF;
 Return Array ($ip, $mark, $ip _start, $ip _end);
Demo:
list ($ip, $mark, $ip _start, $ip _end) = Ip_parse ("192.168.1.12/24");
echo "IP address:", Long2ip ($IP), "\ n";
echo "Subnet mask:", Long2ip ($mark), "\ n";
echo "IP segment start:", Long2ip ($ip _start), "\ n";
echo "IP segment End:", Long2ip ($ip _end), "\ n";

Results:

IP address: 192.168.1.12
subnet Mask: 255.255.255.0
IP segment start: 192.168.1.0
IP segment end: 192.168.1.255

IP is in the IP segment:

function ip_in ($IP, $ip _str) {
 $mark _len =;
 if (Strpos ($ip _str, "/") > 0) {
  list ($ip _str, $mark _len) = Explode ("/", $ip _str);
 }
 $right _len = 32-$mark _len;
 Return Ip2long ($IP) >> $right _len = = Ip2long ($ip _str) >> $right _len
;
Demo:
var_dump (ip_in ("192.168.1.1", "192.168.1.0/24"));

PS: Here again for you to provide an online subnet mask calculation tool for everyone to use for reference:
Online Network Calculator | TCP/IP subnet mask calculation and conversion tool:
Http://tools.jb51.net/aideddesign/ipcalc

More interested in PHP related content readers can view the site topics: "PHP Network Programming Skills Summary", "PHP operation and operator Usage Summary", "PHP file Operation Summary", "Basic PHP Grammar Introduction Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.