Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 146861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:40:43+00:00 2026-05-11T08:40:43+00:00

I’m looking for quick/simple method for matching a given IP4 dotted quad IP to

  • 0

I’m looking for quick/simple method for matching a given IP4 dotted quad IP to a CIDR notation mask.

I have a bunch of IPs I need to see if they match a range of IPs.

example:

$ips = array('10.2.1.100', '10.2.1.101', '10.5.1.100', '1.2.3.4');  foreach ($ips as $IP) {     if (cidr_match($IP, '10.2.0.0/16') == true) {         print 'you're in the 10.2 subnet\n';      } } 

What would cidr_match() look like?

It doesn’t really have to be simple, but fast would be good. Anything that uses only built-in/common functions is a bonus (as I’m likely to get one person to show me something in pear that does this, but I can’t depend on pear or that package being installed where my code is deployed).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T08:40:44+00:00Added an answer on May 11, 2026 at 8:40 am

    If only using IPv4:

    • use ip2long() to convert the IPs and the subnet range into long integers
    • convert the /xx into a subnet mask
    • do a bitwise ‘and’ (i.e. ip & mask)’ and check that that ‘result = subnet’

    something like this should work:

    function cidr_match($ip, $range) {     list ($subnet, $bits) = explode('/', $range);     if ($bits === null) {         $bits = 32;     }     $ip = ip2long($ip);     $subnet = ip2long($subnet);     $mask = -1 << (32 - $bits);     $subnet &= $mask; # nb: in case the supplied subnet wasn't correctly aligned     return ($ip & $mask) == $subnet; } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 209k
  • Answers 209k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The C++ way would be: wchar_t* foo = L"123"; std::wistringstream… May 12, 2026 at 9:43 pm
  • Editorial Team
    Editorial Team added an answer Using QStackedWidget is probably the most standard solution. May 12, 2026 at 9:43 pm
  • Editorial Team
    Editorial Team added an answer You should be able to get a crash log. In… May 12, 2026 at 9:43 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.