PHPackages                             phillarmonic/cpf-cnpj - 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. [Validation &amp; Sanitization](/categories/validation)
4. /
5. phillarmonic/cpf-cnpj

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

phillarmonic/cpf-cnpj
=====================

A PHP library for validating and formatting CPF and CNPJ numbers.

1.0.0(1y ago)317.1k↓51.2%21MITPHP

Since Sep 27Pushed 1y ago1 watchersCompare

[ Source](https://github.com/Phillarmonic/cpf-cnpj)[ Packagist](https://packagist.org/packages/phillarmonic/cpf-cnpj)[ RSS](/packages/phillarmonic-cpf-cnpj/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (1)

phillarmonic/cpf-cnpj
=====================

[](#phillarmoniccpf-cnpj)

A PHP library for validating and formatting CPF and CNPJ numbers from Brazil.

Installation
------------

[](#installation)

You can install this library using Composer. Run the following command in your project directory:

```
composer require phillarmonic/cpf-cnpj
```

Usage
-----

[](#usage)

### Validating CPF Numbers

[](#validating-cpf-numbers)

To validate a CPF number:

```
use Phillarmonic\CpfCnpj\CPF;

$cpf = new CPF('123.456.789-09');
if ($cpf->isValid()) {
    echo "The CPF is valid.";
} else {
    echo "The CPF is not valid.";
}
```

### Formatting CPF Numbers

[](#formatting-cpf-numbers)

To format a valid CPF number:

```
use Phillarmonic\CpfCnpj\CPF;

$cpf = new CPF('12345678909');
$formattedCpf = $cpf->format();

if ($formattedCpf !== false) {
    echo "Formatted CPF: " . $formattedCpf; // Outputs: 123.456.789-09
} else {
    echo "Invalid CPF, unable to format.";
}
```

### Validating CNPJ Numbers

[](#validating-cnpj-numbers)

To validate a CNPJ number:

```
use Phillarmonic\CpfCnpj\CNPJ;

$cnpj = new CNPJ('12.345.678/0001-95');
if ($cnpj->isValid()) {
    echo "The CNPJ is valid.";
} else {
    echo "The CNPJ is not valid.";
}
```

### Formatting CNPJ Numbers

[](#formatting-cnpj-numbers)

To format a valid CNPJ number:

```
use Phillarmonic\CpfCnpj\CNPJ;

$cnpj = new CNPJ('12345678000195');
$formattedCnpj = $cnpj->format();

if ($formattedCnpj !== false) {
    echo "Formatted CNPJ: " . $formattedCnpj; // Outputs: 12.345.678/0001-95
} else {
    echo "Invalid CNPJ, unable to format.";
}
```

Important Notes
---------------

[](#important-notes)

1. The library automatically removes any non-digit characters from the input, so you can pass formatted or unformatted numbers.
2. The `isValid()` method checks for:
    - Correct length (11 digits for CPF, 14 digits for CNPJ)
    - Not in the blacklist of known invalid numbers
    - Validity of check digits
3. The `format()` method returns `false` if the number is invalid.

Error Handling
--------------

[](#error-handling)

The library doesn't throw exceptions. Instead, it returns `false` or fails silently in case of invalid input. Always check the return value of `isValid()` before using the number, and check if `format()` returns `false` before using the formatted result.

Contributing
------------

[](#contributing)

If you'd like to contribute to this project, please submit a pull request on the GitHub repository.

License
-------

[](#license)

This library is open-sourced software licensed under the MIT license.

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 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.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

646d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/160736656?v=4)[Phillarmonic Software](/maintainers/phillarmonic)[@phillarmonic](https://github.com/phillarmonic)

---

Top Contributors

[![phillarmonkey](https://avatars.githubusercontent.com/u/162709247?v=4)](https://github.com/phillarmonkey "phillarmonkey (6 commits)")

---

Tags

cnpjcnpj-validatorcpfcpf-validadorphpphp8validationvalidationformattingcpfcnpj

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/phillarmonic-cpf-cnpj/health.svg)

```
[![Health](https://phpackages.com/badges/phillarmonic-cpf-cnpj/health.svg)](https://phpackages.com/packages/phillarmonic-cpf-cnpj)
```

###  Alternatives

[robersonfaria/validation

Pacote Laravel para validações comuns ao Brasil(pt-BR) tipo: CNPJ,CPF,CEP,CNS

107.6k](/packages/robersonfaria-validation)[bissolli/validador-cpf-cnpj

Classe em PHP para validação de CPF e CNPJ.

2195.0k](/packages/bissolli-validador-cpf-cnpj)[yiibr/yii2-br-validator

Provide validations and features for brazilian localization

15107.1k4](/packages/yiibr-yii2-br-validator)

PHPackages © 2026

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