PHPackages                             sdrobov/php-cidr - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. sdrobov/php-cidr

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

sdrobov/php-cidr
================

Several useful functions available for IPv4 addresses

06.2kPHP

Since Oct 26Pushed 7y agoCompare

[ Source](https://github.com/sdrobov/php-cidr)[ Packagist](https://packagist.org/packages/sdrobov/php-cidr)[ RSS](/packages/sdrobov-php-cidr/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Several useful functions available for IPv4 addresses. It is implemented using mostly bitwise expressions so it should be easily ported to other languages. There is very little done in the way of error checking. References are stated within comment blocks.

Introduction
------------

[](#introduction)

For a midsize project I wanted to store IP ranges in the database with the option to to also store CIDR blocks. CIDR blocks, though powerful are somewhat difficult for a typical user. Additional using them is not as precise for all ip ranges. There are a plethora of [tools](http://ip2cidr.com/ "IP to CIDR tool")available on the internet that will do what I would like. However, incorporating these tools would not be practical. What I want is to mimic the functionality of these tools so that it can be easily imported into any of my projects. The most coveted tool for me would be to convert an IP range to a precise range of CIDR blocks. This required specific functionality not naturally provided in PHP.

- Check for Valid Netmask
- Check whether an IP address is within a CIDR block.
- Take user input and a Netmask and make it into a valid CIDR block.
- CIDR number into Netmask
- Netmask to CIDR
- Take an IP range and fit it into an exact range of CIDR blocks.

This presents some difficulty in that PHP's \[network functions\] [phpnetwork](http://www.php.net/manual/en/ref.network.php "PHP Network Functions") are not thorough enough. The revelation came when I realized that an IP address is merely a number. In fact the whole protocol is rooted in binary using very specific patterns. With that in mind I thought we could develop very light weight methods to solve our problem.

The Code
--------

[](#the-code)

It is important to note that the methods provided are meant for IPv4 addresses only are only tested on a 32bit system. Also, I did not care to do much in the way of error checking, but doing so, like testing whether the CIDR number is unsigned and less than or equal to 32, should be trivial.

Though the solution I sought after would require PHP I didn't limit myself to that language only. In fact the PHP code I found seemed inefficient. Most involved a number conversions or parsing the address using sprintf using loops and nested if statements. Indeed the most efficient code, which shouldn't surprise most was in ANSI C. [Bit Twiddling Hacks](http://graphics.stanford.edu/~seander/bithacks.html "Bit Twiddling Hacks by Sean Eron Anderson") resource proved very useful.

### About Binary

[](#about-binary)

I am not attempting to teach binary math. Since the code does not "read like prose" a small amount of knowledge is required in order to understand the code. Some excellent resources are Wikipedia's article on [CIDR](http://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing "Classless Inter-Domain Routing") and [PHP binary operators](http://www.php.net/manual/en/language.operators.bitwise.php "PHP's Bitwise Operators").

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/0a97b2f7577b50a83ebd7018cb6eab0e21d20b235c2f7f0aee46919bcdb61932?d=identicon)[sdrobov](/maintainers/sdrobov)

---

Top Contributors

[![jonavon](https://avatars.githubusercontent.com/u/1316100?v=4)](https://github.com/jonavon "jonavon (3 commits)")

### Embed Badge

![Health badge](/badges/sdrobov-php-cidr/health.svg)

```
[![Health](https://phpackages.com/badges/sdrobov-php-cidr/health.svg)](https://phpackages.com/packages/sdrobov-php-cidr)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
