|
| 1 | +# Respect\Masker |
| 2 | + |
| 3 | +[](https://github.com/Respect/Masker/actions/workflows/continuous-integration.yml) |
| 4 | +[](https://codecov.io/gh/Respect/Masker) |
| 5 | +[](https://packagist.org/packages/respect/masker) |
| 6 | +[](https://packagist.org/packages/respect/masker) |
| 7 | +[](https://packagist.org/packages/respect/masker) |
| 8 | + |
| 9 | +A powerful and flexible PHP library for masking sensitive text data using intuitive range-based patterns. |
| 10 | + |
| 11 | +Whether you need to hide credit card numbers, mask email addresses, or protect user privacy, Masker gives you precise control over what gets hidden and what stays visible. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +```bash |
| 16 | +composer require respect/masker |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Basic Usage |
| 22 | + |
| 23 | +```php |
| 24 | +use Respect\Masker\TextMasker; |
| 25 | + |
| 26 | +$masker = new TextMasker(); |
| 27 | + |
| 28 | +echo $masker->mask('1234123412341234', '1-3,8-12'); |
| 29 | +// Outputs: ***4123*****1234 |
| 30 | +``` |
| 31 | + |
| 32 | +## API |
| 33 | + |
| 34 | +### `mask` |
| 35 | + |
| 36 | +- `mask(string $input, string $range): string` |
| 37 | +- `mask(string $input, string $range, string $replacement): string` |
| 38 | + |
| 39 | +Masks the input string according to the specified range. |
| 40 | + |
| 41 | +**Parameters:** |
| 42 | + |
| 43 | +- `$input`: The string to mask |
| 44 | +- `$range`: Comma-separated range specifications |
| 45 | +- `$replacement`: The character to use for masking (default `*`) |
| 46 | + |
| 47 | +**Returns:** The masked string |
| 48 | + |
| 49 | +**Throws:** `InvalidArgumentException` when invalid ranges are provided |
| 50 | + |
| 51 | +### `isValidRange` |
| 52 | + |
| 53 | +- `isValidRange(string $range): bool` |
| 54 | + |
| 55 | +Validates whether the mask range specification is syntactically correct. |
| 56 | + |
| 57 | +## Range Syntax |
| 58 | + |
| 59 | +| Pattern | Description | Example | |
| 60 | +| ------- | ------------------------------- | --------------------- | |
| 61 | +| `N` | Single position (1-based) | `3` | |
| 62 | +| `N-` | From position N to end | `1-` | |
| 63 | +| `N-M` | Range from position N to M | `1-3` | |
| 64 | +| `-N` | Last N characters | `-3` | |
| 65 | +| `C-M` | From character C to character M | `1-@` | |
| 66 | +| `\N` | Escaped numeral character | `\5` | |
| 67 | +| `\C` | Escaped special character | `\-`, `\,`, or `\\\\` | |
| 68 | + |
| 69 | +Multiple ranges can be specified using commas: `1-B,6-8,10` |
| 70 | + |
| 71 | +### Numeric Positions (1-based) |
| 72 | + |
| 73 | +Use numeric positions to mask specific characters (1-based indexing). |
| 74 | + |
| 75 | +| Range | Input | Output | |
| 76 | +| ------ | -------------- | -------------- | |
| 77 | +| `1-3` | `password123` | `***ord123` | |
| 78 | +| `1-3` | `'12345'` | `***45` | |
| 79 | +| `7-12` | `'1234567890'` | `123456******` | |
| 80 | + |
| 81 | +#### Character Delimiters |
| 82 | + |
| 83 | +Use character delimiters to mark ranges between characters in the string. |
| 84 | + |
| 85 | +| Range | Input | Output | |
| 86 | +| ------ | --------------------- | --------------------- | |
| 87 | +| `1-@` | `username@domain.com` | `********@domain.com` | |
| 88 | +| `A-\5` | `ABCDD1234567890EFGH` | `*********567890EFGH` | |
| 89 | +| `A-\-` | `ABCD-1234567890EFGH` | `####-1234567890EFGH` | |
| 90 | +| `B-\,` | `ABC,DEF,GHI` | `**C,DEF,GHI` | |
| 91 | + |
| 92 | +#### Multiple Ranges |
| 93 | + |
| 94 | +Combine multiple ranges using commas to mask non-contiguous sections. |
| 95 | + |
| 96 | +| Range | Input | Output | |
| 97 | +| ------------ | -------------------- | --------------------- | |
| 98 | +| `1-3,8-12` | `1234123412341234` | `***4123*****1234` | |
| 99 | +| `1,3,5` | `'12345'` | `*2*4*` | |
| 100 | +| `1-3,6-8,10` | `abcdefghij` | `***de***j` | |
| 101 | +| `1,3,5` | `12345` | `*2*4*` | |
| 102 | +| `1-3,8-12` | `'123456789012'` | `***45678******` | |
| 103 | +| `A-D,5-8` | `ABCD1234567890EFGH` | `####D####567890EFGH` | |
| 104 | +| `1-c,2-5` | `abc123def456` | `#####3def456` | |
| 105 | + |
| 106 | +#### Special Patterns |
| 107 | + |
| 108 | +**Mask to end**: Use `1-` to mask from the beginning to the end |
| 109 | +**Mask last N**: Use `-N` to mask the last N characters |
| 110 | + |
| 111 | +| Range | Input | Output | |
| 112 | +| ----- | ---------- | ---------- | |
| 113 | +| `1-` | `12345678` | `********` | |
| 114 | +| `-2` | `123456` | `1234**` | |
| 115 | +| `-3` | `abcdefgh` | `abcde***` | |
| 116 | + |
| 117 | +#### Escaping Special Characters |
| 118 | + |
| 119 | +Escape special characters with backslash when they need to be used as literals. |
| 120 | + |
| 121 | +| Range | Input | Output | |
| 122 | +| -------- | ---------------------------- | ---------------------------- | |
| 123 | +| `3-\5` | `1234567890` | `12**567890` | |
| 124 | +| `1-\-` | `email-something@domain.com` | `*****-something@domain.com` | |
| 125 | +| `1-\\\\` | `path\to\file` | `****\to\file` | |
| 126 | + |
| 127 | +To use `-`, `\` and `,` as delimiters, you must always add backslashes before them. |
| 128 | + |
| 129 | +#### Unicode Support |
| 130 | + |
| 131 | +Full support for UTF-8 strings including accented characters. |
| 132 | + |
| 133 | +| Range | Input | Output | |
| 134 | +| ------- | ---------------- | ---------------- | |
| 135 | +| `1-` | `oftalmoscópico` | `**************` | |
| 136 | +| `2-6,9` | `áéíóúãõçüñ` | `á*****õç*ñ` | |
| 137 | +| `-4` | `españolñç` | `españolñ*` | |
| 138 | + |
| 139 | +## License |
| 140 | + |
| 141 | +This project is licensed under the ISC License - see the LICENSE file for details. |
0 commit comments