Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

From IP address, extract just the first three octets (network) from the address using PHP

Posted on 2006-06-22
10
Medium Priority
?
624 Views
Last Modified: 2008-02-01
I want to extract just the first three octets (the network basically) from an IP address with PHP.

I have an IP address (i.e.  1.2.3.4)
and I want to extract just the:   1.2.3    part

It could be 1, 2 or 3 numbers per octet (since it's an IP address):
   could be:             I want:
  192.168.0.1   -->  192.168.0
  10.1.102.3    -->   10.1.102

etc.

Thanks.
0
Comment
Question by:ldorazio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 54

Accepted Solution

by:
b0lsc0tt earned 1000 total points
ID: 16964711
ldorazio,

if (strrpos($ip, ".") != False) {
  $strIp = substr($ip, 0, strrpos($ip, "."));
}

In the example above $ip is the ip address.  The function strrchr() will look for the last occurrance of the period.  If it is not found then it will return false so that is why I have the If statement.  If it finds a period in $ip then it will use the position of the last one to get a substring and assign it to $strIp.  If $ip was 192.168.1.100 then $strIp would be 192.168.1

b0lsc0tt
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16964716
Hi ldorazio,

Well, since there are always at least three parts, you can just do:

$ip = '192.168.0.1';
$cut_ip = substr($ip, 0, strrpos($ip, '.'));
echo($cut_ip);

Regards,
Ted
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16964718
Heh, didn't see your post, b0lsc0tt. My bad.

Ted
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 16964749
@Zyloch - No problem!  I have been on the other side often too.  Posted at essentially the same time and I have no problem splitting if our answers help him.

bol
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 16964764
Don't worry about the split. The points are rightfully yours, and I don't care so much for them anyhow :P It's interesting how our answers are essentially the same though, hehe

Ted
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 16964790
@Zyloch, I was not watching what you typed! :D  With your expertise in this area I am glad to see your similar post.  I'll be fine with whatever Idorazio decides to do regarding points.

bol
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16964913
Silly idea, but ... (untested) ...

<?php
$s_ip = '192.168.0.1';
$i_ip = ip2long($s_ip);
$i_ip = $i_ip & ~255;
$s_ip = long2ip($i_ip);
$s_ip = substr($s_ip, 0, -2);
?>

Take the string. Convert it to a long using ip2long(). Mask off the lower 8 bits. Convert it back to an IP which will now end in ".0". Substr to drop off the last 2 characters.

I doubt I'd actually do it this way, but an exercise all the same.

And at least it is actually using a mask! So MAYBE has more relevance than just string manipulation.

0
 

Author Comment

by:ldorazio
ID: 16965123
Awesome, thank you!

Sometimes I can't think the right way, and I was in a hurry so I appreciate the quick answers.
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16966767
A completely useless example of masking. I had a spare 10 mins whilst a backup was running.

<?php
function maskIP($s_IP, $i_bits)
      {
      $i_IP = ip2long($s_IP);
      $i_mask = ($i_bits > 0) ? pow(2, $i_bits) - 1 : 0;
      $i_IP &= ~$i_mask;
      $s_IP = long2ip($i_IP);
      return $s_IP;
      }

$s_IP = rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255) . '.' . rand(0, 255);
$i_mask = rand(0, 32);
echo $s_IP . ' mask ' . $i_mask . ' is ' . maskIP($s_IP, $i_mask) . "\n";
?>
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 16970649
Your welcome!  I'm glad that I could help you and I enjoyed participating with the others in this.  Thank you for the grade, the points and the fun question.

bol
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question