bvp / trimmer
BVP Trimmer for strings, arrays, and objects.
Installs: 24 246
Dependents: 6
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- myclabs/deep-copy: ^1.11
Requires (Dev)
- phpunit/phpunit: ^10.1 || ^11.1 || ^12.0
This package is auto-updated.
Last update: 2025-08-24 16:20:31 UTC
README
BVP Trimmer is a PHP library that extends the built-in functions trim
, ltrim
, and rtrim
and allows you to recursively trim arrays and objects as well.
📦 Requirements
- PHP: ^8.2
- myclabs/deep-copy: ^1.11
💾 Installation
composer require bvp/trimmer
⚡ Usage
Supported Methods
Method | Description | Parameters |
---|---|---|
Trimmer::trim($value, $characters = null) |
Trims strings, arrays, and objects | $value : string | array | object$characters : Characters to remove (optional) |
Trimmer::ltrim($value, $characters = null) |
Left-side trimming | Same as above |
Trimmer::rtrim($value, $characters = null) |
Right-side trimming | Same as above |
Basic Usage
<?php require __DIR__ . '/vendor/autoload.php'; use BVP\Trimmer\Trimmer; // Trim strings echo Trimmer::trim(' trimmer '); // "trimmer" echo Trimmer::trim(' @trimmer@ ', "\x20\x40"); // "trimmer" // Left-side trim only echo Trimmer::ltrim(' trimmer '); // "trimmer " echo Trimmer::ltrim(' @trimmer@ ', "\x20\x40"); // "trimmer@ " // Right-side trim only echo Trimmer::rtrim(' trimmer '); // " trimmer" echo Trimmer::rtrim(' @trimmer@ ', "\x20\x40"); // " @trimmer"
Trimmer::trim() - Trimming Arrays
$result = Trimmer::trim([' trimmerA ']); print_r($result); // Output: Array ( [0] => trimmerA )
$result = Trimmer::trim([' trimmerA ', [' trimmerB ']]); print_r($result); // Output: Array ( [0] => trimmerA [1] => Array ( [0] => trimmerB ) )
$result = Trimmer::trim([' trimmerA ', 1, 1.0, true, null]); print_r($result); // Output: Array ( [0] => trimmerA [1] => 1 [2] => 1 [3] => 1 [4] => )
Trimmer::trim() - Trimming Objects
To trim object properties, you must provide getter and setter methods. Nested objects are also supported.
$objectA = new class { private string $propertyA = ' trimmerA '; private string $propertyB = ' trimmerB '; // Will NOT be trimmed public function getPropertyA(): string { return $this->propertyA; } public function setPropertyA(string $value): void { $this->propertyA = $value; } public function getPropertyB(): string { return $this->propertyB; } }; Trimmer::trim($objectA); // $propertyA will be trimmed, but $propertyB remains unchanged
Nested objects are also supported:
$objectB = new class($objectA) { private string $propertyC = ' trimmerC '; private string $propertyD = ' trimmerD '; // Will NOT be trimmed private object $objectA; public function __construct(object $objectA) { $this->objectA = $objectA; } public function getPropertyC(): string { return $this->propertyC; } public function setPropertyC(string $value): void { $this->propertyC = $value; } public function getPropertyD(): string { return $this->propertyD; } public function getObjectA(): object { return $this->objectA; } }; Trimmer::trim($objectB); // $propertyC and $objectA->propertyA will be trimmed, // but $propertyD and $objectA->propertyB remain unchanged
⚠️ Notes
Trimmer::trim
,Trimmer::ltrim
, andTrimmer::rtrim
are non-destructive. They return new values without modifying the originals.- Object properties without both getter and setter methods cannot be trimmed.
📄 License
BVP Trimmer is open-source software released under the MIT license.