PHPackages                             bvp/trimmer - 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. bvp/trimmer

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

bvp/trimmer
===========

Recursively trims whitespace (or custom characters) from strings, including strings nested inside arrays of any depth.

10.0.1(1mo ago)0129.9k↑28.8%6MITPHPPHP ^8.1CI passing

Since Aug 15Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/boatracevibeproject/trimmer)[ Packagist](https://packagist.org/packages/bvp/trimmer)[ RSS](/packages/bvp-trimmer/feed)WikiDiscussions main Synced 2w ago

READMEChangelog (2)Dependencies (7)Versions (13)Used By (6)

Trimmer
=======

[](#trimmer)

[English](README.md) | [日本語](README_ja.md)

[![php](https://camo.githubusercontent.com/75108c5e4583d1b185ecda50819a7edac79a53561968e48dcafed49cdcf6765a/68747470733a2f2f706f7365722e707567782e6f72672f6276702f7472696d6d65722f726571756972652f706870)](https://packagist.org/packages/bvp/trimmer)[![stable](https://camo.githubusercontent.com/3be8064dbec2f755855b9a144f4267f6e8255ef386ca0290eb11c6631dc0b0c0/68747470733a2f2f706f7365722e707567782e6f72672f6276702f7472696d6d65722f762f737461626c65)](https://packagist.org/packages/bvp/trimmer)[![license](https://camo.githubusercontent.com/de75e1a03179c382dc205ddcfeb924052a6424f2f1bfd117a522b698895683ee/68747470733a2f2f706f7365722e707567782e6f72672f6276702f7472696d6d65722f6c6963656e7365)](https://packagist.org/packages/bvp/trimmer)

[![test](https://github.com/boatracevibeproject/trimmer/actions/workflows/test.yml/badge.svg)](https://github.com/boatracevibeproject/trimmer/actions/workflows/test.yml)[![psalm](https://github.com/boatracevibeproject/trimmer/actions/workflows/psalm.yml/badge.svg)](https://github.com/boatracevibeproject/trimmer/actions/workflows/psalm.yml)[![audit](https://github.com/boatracevibeproject/trimmer/actions/workflows/audit.yml/badge.svg)](https://github.com/boatracevibeproject/trimmer/actions/workflows/audit.yml)[![keepalive](https://github.com/boatracevibeproject/trimmer/actions/workflows/keepalive.yml/badge.svg)](https://github.com/boatracevibeproject/trimmer/actions/workflows/keepalive.yml)[![dependabot-updates](https://github.com/boatracevibeproject/trimmer/actions/workflows/dependabot/dependabot-updates/badge.svg)](https://github.com/boatracevibeproject/trimmer/actions/workflows/dependabot/dependabot-updates)

A small utility class that recursively trims whitespace (or custom characters) from strings, including strings nested inside arrays of any depth.

Why
---

[](#why)

PHP's built-in `trim()` / `ltrim()` / `rtrim()` only operate on a single string. Trimming every string value inside a (possibly nested) array normally means writing your own recursive helper, or reaching for `array_map()` — which breaks as soon as the array contains nested arrays or non-string values.

`Trimmer` handles this for you.

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

[](#installation)

```
composer require bvp/trimmer
```

Usage
-----

[](#usage)

```
use BVP\Trimmer\Trimmer;

Trimmer::trim('  hello  ');
// 'hello'

Trimmer::trim([' foo ', ' bar ', ['  baz  ', null, 42]]);
// ['foo', 'bar', ['baz', null, 42]]
```

### Available methods

[](#available-methods)

MethodBehavior`Trimmer::trim($items, $characters = null, $encoding = null, $trimKeys = false)`Trims both ends`Trimmer::ltrim(...)`Trims the left end only`Trimmer::rtrim(...)`Trims the right end only`Trimmer::trimStart(...)`Alias for `ltrim()``Trimmer::trimEnd(...)`Alias for `rtrim()`All methods accept the same four parameters:

- **`$items`** *(mixed)* — a string, an array (nested to any depth), or any other value. Strings are trimmed; arrays are walked recursively; anything else is returned untouched.
- **`$characters`** *(?string)* — the set of characters to trim. Defaults to the standard whitespace characters (`" \t\n\r\0\x0B"`), same as PHP's built-in `trim()`.
- **`$encoding`** *(?string)* — character encoding, only used on PHP ≥ 8.4 (see below).
- **`$trimKeys`** *(bool, default `false`)* — when `true`, string array keys are trimmed as well (see [Trimming array keys](#trimming-array-keys)).

### PHP version behavior

[](#php-version-behavior)

- **PHP ≥ 8.4**: uses the multibyte-aware `mb_trim()` / `mb_ltrim()` / `mb_rtrim()`, so `$encoding` is respected.
- **PHP &lt; 8.4**: falls back to the built-in `trim()` / `ltrim()` / `rtrim()` (byte-based); `$encoding` is ignored.

### Trimming array keys

[](#trimming-array-keys)

By default, only array *values* are trimmed — keys are left as-is, matching how a plain `array_map('trim', $array)` would behave.

Pass `trimKeys: true` to also trim string keys:

```
Trimmer::trim([' foo ' => ' bar ', 'foo' => 'baz'], trimKeys: true);
// ['foo' => 'baz']
```

**Note on collisions:** if trimming causes two keys to become identical (including PHP's automatic casting of a canonical numeric string key like `"8"` to the integer `8`), the later value silently overwrites the earlier one — the same behavior you'd get from a normal PHP array assignment (`$array[$key] = $value`).

### Maximum nesting depth

[](#maximum-nesting-depth)

Nested arrays are walked recursively, so an array nested deeper than `Trimmer::MAX_DEPTH` (`512`, matching PHP's own `json_decode()` / `json_encode()` default) throws `OverflowException` instead of risking a stack overflow.

What it does not do
-------------------

[](#what-it-does-not-do)

- It does not trim object properties. Objects are returned untouched, since trimming them safely would require knowing each class's getters/setters, `readonly` properties, and constructor validation — which can't be handled generically without risking broken invariants.

License
-------

[](#license)

Trimmer is open-source software released under the [MIT license](LICENSE).

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance94

Actively maintained with recent releases

Popularity33

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 90.7% 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 ~66 days

Recently: every ~81 days

Total

6

Last Release

33d ago

Major Versions

5.x-dev → 6.0.02025-12-31

6.x-dev → 10.0.02026-07-12

PHP version history (2 changes)5.3.0PHP ^8.2

10.0.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/12474425?v=4)[Yuichi Shimo](/maintainers/shimomo)[@shimomo](https://github.com/shimomo)

---

Top Contributors

[![shimomo](https://avatars.githubusercontent.com/u/12474425?v=4)](https://github.com/shimomo "shimomo (146 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (15 commits)")

---

Tags

phptrimtrimmertrimminglibrarytrimmerboatrace

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/bvp-trimmer/health.svg)

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

###  Alternatives

[dedoc/scramble

Automatic generation of API documentation for Laravel applications.

2.2k12.6M140](/packages/dedoc-scramble)[azuracast/azuracast

The AzuraCast self-hosted web radio station management suite.

4.0k27.9k](/packages/azuracast-azuracast)[league/iso3166

ISO 3166-1 PHP Library

69939.1M148](/packages/league-iso3166)[phpactor/phpactor

PHP refactoring and intellisense tool for text editors

1.9k17.9k1](/packages/phpactor-phpactor)

PHPackages © 2026

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