PHPackages                             linna/filter - 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. [Search &amp; Filtering](/categories/search)
4. /
5. linna/filter

ActiveLibrary[Search &amp; Filtering](/categories/search)

linna/filter
============

Linna Filter

v0.3.0(7y ago)442[1 PRs](https://github.com/linna/filter/pulls)MITPHPPHP ^7.1

Since May 29Pushed 7y ago3 watchersCompare

[ Source](https://github.com/linna/filter)[ Packagist](https://packagist.org/packages/linna/filter)[ Docs](https://github.com/linna/filter)[ RSS](/packages/linna-filter/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (3)Versions (6)Used By (0)

 [![Linna Logo](logo-linna-96.png)](#)

 [![Linna dotenv Logo](logo-filter.png)](#)

[![Build Status](https://camo.githubusercontent.com/2b6766b0b89fa2a7742924a18d95113d64c22cffe9d10e34b53dee35507fd3e1/68747470733a2f2f7472617669732d63692e6f72672f6c696e6e612f66696c7465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/linna/filter)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b38f33c8ccf7a635cec1a0271ec7f8afe4c1efa139eaefdb283ea4b7028b64cf/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c696e6e612f66696c7465722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/linna/filter/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/7873d5ab6f793637b0c6044aadbbd8db0c88206f8f5cb1b37681bd91817d82fd/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6c696e6e612f66696c7465722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/linna/filter/?branch=master)[![StyleCI](https://camo.githubusercontent.com/f1be0ab8df4d3b2220a4c6b10637d590f5496882fd764a3425c3d86cf7c6bd08/68747470733a2f2f7374796c6563692e696f2f7265706f732f3131313332313132382f736869656c643f6272616e63683d6d6173746572267374796c653d666c6174)](https://styleci.io/repos/111321128)[![PHP 7.2](https://camo.githubusercontent.com/cfd628dc653e38ca05e99444ae5189ffdbfc80c23e828332633fc9f866763d93/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d372e322d3838393242462e737667)](http://php.net)

About
=====

[](#about)

This package provide filters for validate and sanitize user input data.

Requirements
============

[](#requirements)

This package require php 7.2

Installation
============

[](#installation)

With composer:

```
composer require linna/filter

```

Available Filters
=================

[](#available-filters)

Filters
-------

[](#filters)

Rule NameAliasesDescriptionRule ArgumentsOperatorsExample Data from `$_POST`Example Ruledatedat, dcheck for a valid date1none`['born'] = '1980-06-01'``'born: date Y-m-d'`datecomparedatcmp, dccompare one date with another3&gt;, &lt;, &gt;=, &lt;=, =`['born'] = '1980-06-01'``'born: datecompare < Y-m-d 1990-01-01'`emailmail, e@check for a valid email0none`['email'] = 'foo@mail.com'``'email: email'`escapeescp, esconvert special chars in html entities0none`['name'] = 'foo'``'name: escape'`ipipcheck for a valid ip (ipv4 and ipv4)0none`['host'] = 192.168.0.1``'host: ip'`iprangeiprng, iprcheck if provided ipv4 or ipv6 is in CIDR range1none`['host'] = 192.168.0.1``'host: iprange 192.168.0.1/24'`numbernum, ncheck for a valid number and cast to number0none`['age'] = 25``'age: number'`numbercomparenumcmp, nccompare one number with another2&gt;, &lt;, &gt;=, &lt;=, =`['age'] = 25``'age: numbercompare > 18'`numberintervalnumint, nicheck if a number is included or not on interval3&lt;&gt;, &gt;&lt;, &lt;=&gt;, &gt;=&lt;`['age'] = 25``'age: numberinterval >< 18 80'`requiredreq, rqcheck for null values0none`['name'] = 'foo'``'name: required'`strstring, scast to string0none`['name'] = 'foo'``'name: str'`stringlencomparestrlencmp, strlen, slccheck the length of a string2&gt;, &lt;, &gt;=, &lt;=, =, !=`['name'] = 'foo'``'name: stringlencompare > 2'`A rule could be called with the name or with the alias. An alias help to write rules more quickly.

```
//using rule name
$rule = 'age: number, numbercompare < 30';

//using alias
$rule = 'age: n, nc < 30';
```

Operators
---------

[](#operators)

Rule NameOperatorDescriptionNotesDateCompare&lt;less than&gt;greater than&lt;=less than or equal&gt;=greater than or equal=equalPHP === equalNumberCompare&lt;less than&gt;greater than&lt;=less than or equal&gt;=greater than or equalNumberInterval&lt;&gt;out interval, exclusive8-10: 7, 11 true - 8, 9, 10 false&gt;&lt;in interval, exclusive8-10: 9 true - 7, 8, 10, 11 false&lt;=&gt;out interval, inclusive8-10: 7, 8, 10, 11 true - 9 false&gt;=&lt;in interval, inclusive8-10: 8, 9, 10 true - 7, 11 falseStringLenCompare&lt;length less thanPHP strlen(string) &lt; number&gt;length greater thanPHP strlen(string) &gt; number&lt;=length less than or equalPHP strlen(string) &lt;= number&gt;=length greather than or equalPHP strlen(string) &gt;= number=length equalPHP strlen(string) === number!=length not equalPHP strlen(string) !== numberType of filters
===============

[](#type-of-filters)

Filters can be of two types: Validation filters or Sanitization filters. Validation filters check only if the data respect a certain criterion, instead sanitization filters alter passed data to make them conform to a given rule.

In this package are sanitization filters only **Number** and **Escape**

> **Note:** For security reasons a Validation filter should be preferred, don't try to sanitize a bad user input, discard it!

Usage
=====

[](#usage)

Filters can be used in two different ways.

Filter one field
----------------

[](#filter-one-field)

Apply one or more rules to one value:

```
use Linna\Filter\Filter;

$f = new Filter();
$f->filter(20, 'number numberinterval >< 15 25');

//int 0
var_dump($f->getErrors());

//array (size=1)
//  'data' =>
//    array (size=0)
//      empty
var_dump($f->getMessages());

//array (size=1)
//  'data' => int 20
var_dump($f->getData());
```

Filter multiple fields
----------------------

[](#filter-multiple-fields)

Apply one or more rules to many values, it is useful for validating forms:

```
use Linna\Filter\Filter;

//override $_POST superglobal for simulate data from user form
$_POST = [
    'email' => 'user@email.com',
    'password' => 'p4ssw0rd200!',
    'age' => '25',
    'born' => '1980-06-01',
];

//create instance
$fm = new Filter();
$fm->filter($_POST, [
    'email: required, email',
    'password: required, stringlencompare >= 12',
    'age: number, numbercompare < 30',
    'born: date Y-m-d, datecompare < 15 25');

$errors = $filter->getErrors();
$messages = $filter->getMessages();
$data = $filter->getData();
```

Using result object:

```
use Linna\Filter\Filter;

$f = new Filter();
$result = $f->filter(20, 'number numberinterval >< 15 25');

//or with a single expression
$result = (new Filter())->filter(20, 'number numberinterval >< 15 25');

$errors = $result->errors();
$messages = $result->messages();
$data = $result->data();
```

Rule syntax
===========

[](#rule-syntax)

Parser can accept rules formatted in varius way.

First word must be the name of the input, same present as index in input array.

```
//override $_POST superglobal for simulate data from user form
$_POST = [
    'email' => 'pippo@gmail.com',
    'password' => 'p4ssw0rd200!',
    'age' => '25',
    'born' => '1980-06-01',
];

$rules = [
    'email required email',
    'password required stringlencompare >= 12',
    'age number numberinterval >=< 20 30',
    'born date Y-m-d datecompare = 12',
    'age: number numberinterval >=< 20 30',
    'born: date Y-m-d datecompare = 12',
    'age: number, numberinterval >=< 20 30',
    'born: date Y-m-d, datecompare =, 12',
    'age: number; numberinterval >=addCustomRules($customRules);

//test passed
$r = $filter->filter('Hello World', 'hellocheck');

//array (size=1)
//  'data' => string 'Hello World' (length=11)
var_dump($r->data());

//int 0
var_dump($r->errors());

//array (size=1)
//  'data' =>
//    array (size=0)
//      empty
var_dump($r->messages());

//test fails
$r = $filter->filter('Heo World', 'hellocheck');

//array (size=1)
//  'data' => string 'Heo World' (length=9)
var_dump($r->data());

//int 1
var_dump($r->errors());

//array (size=1)
//  'data' =>
//    array (size=1)
//      0 => string 'Value provided not pass CustomRule (hellocheck) test' (length=52)
var_dump($r->messages());
```

Sanitize
--------

[](#sanitize)

```
$customRules = [];
$customRules[] = new CustomRule(
    //alias
    ['emailtoletters'],
    //callback
    //replace dot and at chars with literal name
    function (string &$received): void {
        $received = str_replace('@', ' at ', $received);
        $received = str_replace('.', ' dot ', $received);
    }
);

$filter = new Filter();
$filter->addCustomRules($customRules);

$r = $filter->filter('sebastian.rapetti@alice.it', 'emailtoletters');

//array (size=1)
//  'data' => string 'sebastian dot rapetti at alice dot it' (length=37)
var_dump($r->data());

//int 0
var_dump($r->errors());

//array (size=1)
//  'data' =>
//    array (size=0)
//      empty
var_dump($r->messages());
```

Custom Rule should have:

- At least one alias.

And for callback function:

- At least one argument, rapresenting the received value.
- Return type, bool or void.

> **Note:** For implementing a sanitize custom rule, closure must have only one argument and this argument must be passed for reference.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98.6% 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

Every ~64 days

Total

3

Last Release

2774d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3b0180939371d6dbe2ab2d18829614d90a995b833d6bb7f503a11766f8d130f0?d=identicon)[s3b4stian](/maintainers/s3b4stian)

---

Top Contributors

[![s3b4stian](https://avatars.githubusercontent.com/u/11441761?v=4)](https://github.com/s3b4stian "s3b4stian (142 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

filtersanitization-filtersvalidation-filtersphpvalidatorfilterform

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/linna-filter/health.svg)

```
[![Health](https://phpackages.com/badges/linna-filter/health.svg)](https://phpackages.com/packages/linna-filter)
```

###  Alternatives

[aura/filter

Filters to validate and sanitize objects and arrays.

173561.0k10](/packages/aura-filter)[wandersonwhcr/romans

A Simple PHP Roman Numerals Library

44395.1k8](/packages/wandersonwhcr-romans)

PHPackages © 2026

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