|
| 1 | +<!-- |
| 2 | +SPDX-FileCopyrightText: (c) Respect Project Contributors |
| 3 | +SPDX-License-Identifier: ISC |
| 4 | +SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com> |
| 5 | +--> |
| 6 | + |
| 7 | +# TrimFormatter |
| 8 | + |
| 9 | +The `TrimFormatter` removes characters from the edges of strings with configurable masking and side selection, fully supporting UTF-8 Unicode characters. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +### Basic Usage |
| 14 | + |
| 15 | +```php |
| 16 | +use Respect\StringFormatter\TrimFormatter; |
| 17 | + |
| 18 | +$formatter = new TrimFormatter(); |
| 19 | + |
| 20 | +echo $formatter->format(' hello world '); |
| 21 | +// Outputs: "hello world" |
| 22 | +``` |
| 23 | + |
| 24 | +### Trim Specific Side |
| 25 | + |
| 26 | +```php |
| 27 | +use Respect\StringFormatter\TrimFormatter; |
| 28 | + |
| 29 | +$formatter = new TrimFormatter(' ', 'left'); |
| 30 | + |
| 31 | +echo $formatter->format(' hello '); |
| 32 | +// Outputs: "hello " |
| 33 | + |
| 34 | +$formatterRight = new TrimFormatter(' ', 'right'); |
| 35 | + |
| 36 | +echo $formatterRight->format(' hello '); |
| 37 | +// Outputs: " hello" |
| 38 | +``` |
| 39 | + |
| 40 | +### Custom Mask |
| 41 | + |
| 42 | +```php |
| 43 | +use Respect\StringFormatter\TrimFormatter; |
| 44 | + |
| 45 | +$formatter = new TrimFormatter('-._'); |
| 46 | + |
| 47 | +echo $formatter->format('---hello---'); |
| 48 | +// Outputs: "hello" |
| 49 | + |
| 50 | +echo $formatter->format('._hello_._'); |
| 51 | +// Outputs: "hello" |
| 52 | +``` |
| 53 | + |
| 54 | +### Unicode Characters |
| 55 | + |
| 56 | +```php |
| 57 | +use Respect\StringFormatter\TrimFormatter; |
| 58 | + |
| 59 | +// Trim CJK full-width spaces |
| 60 | +$formatter = new TrimFormatter(' '); |
| 61 | + |
| 62 | +echo $formatter->format(' hello世界 '); |
| 63 | +// Outputs: "hello世界" |
| 64 | + |
| 65 | +// Trim emoji |
| 66 | +$formatterEmoji = new TrimFormatter('😊'); |
| 67 | + |
| 68 | +echo $formatterEmoji->format('😊hello😊'); |
| 69 | +// Outputs: "hello" |
| 70 | +``` |
| 71 | + |
| 72 | +## API |
| 73 | + |
| 74 | +### `TrimFormatter::__construct` |
| 75 | + |
| 76 | +- `__construct(string $mask = " \t\n\r\0\x0B", string $side = "both")` |
| 77 | + |
| 78 | +Creates a new trim formatter instance. |
| 79 | + |
| 80 | +**Parameters:** |
| 81 | + |
| 82 | +- `$mask`: The characters to trim from the string edges (default: whitespace characters) |
| 83 | +- `$side`: Which side(s) to trim: "left", "right", or "both" (default: "both") |
| 84 | + |
| 85 | +**Throws:** `InvalidFormatterException` when `$side` is not "left", "right", or "both" |
| 86 | + |
| 87 | +### `format` |
| 88 | + |
| 89 | +- `format(string $input): string` |
| 90 | + |
| 91 | +Removes characters from the specified side(s) of the input string. |
| 92 | + |
| 93 | +**Parameters:** |
| 94 | + |
| 95 | +- `$input`: The string to trim |
| 96 | + |
| 97 | +**Returns:** The trimmed string |
| 98 | + |
| 99 | +## Examples |
| 100 | + |
| 101 | +| Configuration | Input | Output | Description | |
| 102 | +| ------------------ | --------------- | ------------ | ------------------------------- | |
| 103 | +| default | `" hello "` | `"hello"` | Trim spaces from both sides | |
| 104 | +| `"left"` | `" hello "` | `"hello "` | Trim spaces from left only | |
| 105 | +| `"right"` | `" hello "` | `" hello"` | Trim spaces from right only | |
| 106 | +| `"-"` | `"---hello---"` | `"hello"` | Trim hyphens from both sides | |
| 107 | +| `"-._"` | `"-._hello_.-"` | `"hello"` | Trim multiple custom characters | |
| 108 | +| `":"` (`"left"`) | `":::hello:::"` | `"hello:::"` | Trim colons from left only | |
| 109 | +| `" "` (CJK space) | `" hello"` | `"hello"` | Trim CJK full-width space | |
| 110 | +| `"😊"` | `"😊hello😊"` | `"hello"` | Trim emoji | |
| 111 | + |
| 112 | +## Notes |
| 113 | + |
| 114 | +- Fully UTF-8 aware - handles all Unicode scripts including CJK, emoji, and complex characters |
| 115 | +- Special regex characters in the mask (e.g., `.`, `*`, `?`, `+`) are automatically escaped |
| 116 | +- Empty strings return empty strings |
| 117 | +- If the mask is empty or contains no characters present in the input, the string is returned unchanged |
| 118 | +- Trimming operations are character-oriented, not byte-oriented |
| 119 | +- Combining characters are handled correctly (trimming considers the full character sequence) |
| 120 | + |
| 121 | +### Default Mask |
| 122 | + |
| 123 | +The default mask includes standard whitespace characters: |
| 124 | + |
| 125 | +- ` `: ASCII SP character 0x20, an ordinary space. |
| 126 | +- `\t`: ASCII HT character 0x09, a tab. |
| 127 | +- `\n`: ASCII LF character 0x0A, a new line (line feed). |
| 128 | +- `\r`: ASCII CR character 0x0D, a carriage return. |
| 129 | +- `\0`: ASCII NUL character 0x00, the NUL-byte. |
| 130 | +- `\v`: ASCII VT character 0x0B, a vertical tab. |
0 commit comments