PHPackages                             srcoder/normalize-strings - 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. srcoder/normalize-strings

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

srcoder/normalize-strings
=========================

Re-usable way of normalizing strings

1.1.1(8y ago)53.3k11MITPHPPHP ^7.0

Since Aug 23Pushed 8y ago1 watchersCompare

[ Source](https://github.com/JeroenBoersma/php-normalize)[ Packagist](https://packagist.org/packages/srcoder/normalize-strings)[ RSS](/packages/srcoder-normalize-strings/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependencies (1)Versions (6)Used By (1)

Srcoder\\Normalize strings library
==================================

[](#srcodernormalize-strings-library)

Use to normalize strings. Implements most basic conversions. Find a new one, please create a PR :-)

Dependencies
------------

[](#dependencies)

Requires minimum PHP 7.0+

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

[](#installation)

```
composer require srcoder/normalize-strings
```

Rules
-----

[](#rules)

- `Srcoder\Normalize\Rule\Append(string $append)`
- `Srcoder\Normalize\Rule\Prepend(string $prepend)`
- `Srcoder\Normalize\Rule\Lowercase`
- `Srcoder\Normalize\Rule\Uppercase`
- `Srcoder\Normalize\Rule\Replace($search, $replace)`
- `Srcoder\Normalize\Rule\Trim(string $chars)`
- `Srcoder\Normalize\Rule\Words(string $delimiters)`
- `Srcoder\Normalize\Rule\RegExp(string $pattern, string $replacement)`
- `Srcoder\Normalize\Rule\Callback(\Closure $closure)`
- `Srcoder\Normalize\Rule\Safe(RuleInterface $rule, int $limit = -1)`

Caching
-------

[](#caching)

Normalized strings are cached internally, second lookup for the same string will be returned from cache.

Basic usage
-----------

[](#basic-usage)

This is basic usage of the normlizer.

```
$normalizer = new Srcoder\Normalize\Normalize([
    new Srcoder\Normalize\Rule\Uppercase,
    new Srcoder\Normalize\Rule\Append(' World'),
    new Srcoder\Normalize\Rule\Append('!')
]);

echo $normalizer->normalize('Hello');
// "HELLO World!"

// Adding rules
$normalizer->addRule(new Srcoder\Normalize\Rule\Replace('HELLO', 'Bye'));

echo $normalizer->normalize('Hello');
// "Bye World!"
```

Chaining
--------

[](#chaining)

You can chain normalizer, if it only adds a simple thingy.

```
// ... continue

$newNormalizer = new Srcoder\Normalize\Normalize([
    new Srcoder\Normalize\Rule\Prepend('Good')
]);

echo $newNormalizer->normalize('Hello');
// "GoodHello"

// Set chain
$newNormalizer->setChain($normalizer);

echo $newNormalizer->normalize('Hello');
// "GoodBye World!"
```

Add-/prepend-Rule
-----------------

[](#add-prepend-rule)

You can add or prepend rules to a normalizer.

```
// ... continue

$newNormalizer->prependRule(new Srcoder\Normalize\RegExp('#[A-Z]+#', '+'));

echo $newNormalizer->normalize('Hello');
// "GoodBye+ELLO World!"
```

Multiple rules at once
----------------------

[](#multiple-rules-at-once)

Just as in the constructor you can add multiple rules.

- `addRules([Rule, Rule])`
- `prependRules([Rule, Rule])`

Adding your own rules
---------------------

[](#adding-your-own-rules)

Just implement the `Sroder\Normalize\Rule\RuleInterface`

```
class sha1Rule implements Sroder\Normalize\Rule\RuleInterface
{

    public function apply(string $string) : string
    {
        return sha1($strings);
    }

}

$myNormalizer = new Sroder\Normalize([
    new sha1Rule
]);

echo $myNormalizer->normalize('test');
// "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
```

Manager (instance)
------------------

[](#manager-instance)

If you need your normalizers to be available everywhere.

```
// ... continue

$normalizeManager = new Srcoder\Normalize\Manager();
// or static
$normalizeManager = Srcoder\Normalize\Manager::instance();

// Add already defined normalizer
$normalizeManager->add($normalizer, 'helloworld');

// createAndAdd
$normalizeManager->createAndAdd(
        [ // Rules
            new Srcoder\Normalize\Rule\RegExp("#[_ ]*([A-Z])#", "_\\1"),
            new Srcoder\Normalize\Rule\Trim("_ \t\n\r\0\x0B"),
            new Srcoder\Normalize\Rule\Lowercase()
        ],
        'underscore' // identifier
        //, 'helloworld' // chain
);

// ... snip to somewhere else

echo $normalizeManager->get('helloworld')
        ->normalize('Ibiza!');
// "Bye Ibiza!!"

echo Srcoder\Normalize\Manager::instance()
        ->get('underscore')
        ->normalize('HelloWorld');
// "hello_world"
```

Trait
-----

[](#trait)

To use the normalizer in any of your classes you can use the trait.

```
class MyAwesomeClass
{
    use \Srcoder\Normalize\NormalizeTrait;

    public function __construct()
    {
        $this->normalizerInit();

        $this->addNormalizeRules(
                [
                    new Srcoder\Normalize\Rule\Trim('Ho'),
                    new Srcoder\Normalize\Rule\Uppercase,
                ]
        );
    }
}

$myAwesome = new MyAwesomeClass;

echo $myAwesome->normalize('Hello');
// "ELL"
```

Tests
-----

[](#tests)

All code is covered by tests, if you want to create a PR please run these tests too.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 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

Every ~2 days

Total

5

Last Release

3184d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1548630acefd0c1ac8002a37135d67741307f978296ac628422e3923b7a3766b?d=identicon)[JeroenBoersma](/maintainers/JeroenBoersma)

---

Top Contributors

[![JeroenBoersma](https://avatars.githubusercontent.com/u/1163348?v=4)](https://github.com/JeroenBoersma "JeroenBoersma (18 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/srcoder-normalize-strings/health.svg)

```
[![Health](https://phpackages.com/badges/srcoder-normalize-strings/health.svg)](https://phpackages.com/packages/srcoder-normalize-strings)
```

###  Alternatives

[happy-types/enumerable-type

Strongly typed implementation of enumerable type in PHP which helps us to write a safer more readable code.

4742.6k](/packages/happy-types-enumerable-type)

PHPackages © 2026

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