PHP Luminova: Mask Values for Display
Hide portions of strings, email addresses, phone numbers, and card numbers before displaying them.
Use Mask to display part of a value, such as the final four digits of a payment card. The methods return display strings and count bytes where they slice the input. Avoid logging passwords or using the display result as a substitute for secure storage.
Usage
use Luminova\Utility\Mask;
echo Mask::email('[email protected]'); // am***@example.com
echo Mask::card('4111 1111 1111 1234'); // **** **** **** 1234Class Definition
- Full namespace:
Luminova\Utility\Mask - This class is final.
Methods
position
Mask a string by position to hide part of it (e.g: mfk---*kld).
public static function position(
string $string,
string $masker = '*',
string $position = 'center',
?int $limit = null
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$string | string | String to mask. |
$masker | string | Mask character (default: "*"). |
$position | string | Masking position ("center", "left", or "right") (default: 'center'). |
$limit | int\|null | Maximum number of characters to mask (default: null). |
Return:
string - Return masked string.
Examples:
use Luminova\Utility\Mask;
echo Mask::position('ABCDEFGH'); // ABC**FGH
echo Mask::position('ABCDEFGH', position: 'left', limit: 3); // ***DEFGH
echo Mask::position('ABCDEFGH', masker: '#', position: 'right', limit: 3); // ABCDE###Mask an email address to hide part of it (e.g [email protected]).
public static function email(
string $email,
string $masker = '*',
int $visible = 2
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$email | string | Email address to mask. |
$masker | string | Mask character (default: "*"). |
$visible | int | Number of visible characters before masking (default: 2). |
Return:
string - Return the masked email address.
Examples:
use Luminova\Utility\Mask;
echo Mask::email('[email protected]'); // am***@example.com
echo Mask::email('[email protected]', masker: '-', visible: 1); // [email protected]phone
Keep the ending bytes visible and replace the preceding bytes with the mask. Input punctuation participates in the byte count. Use a positive visible count shorter than the input for partial masking. A count of zero or less masks the entire input.
public static function phone(
string $phone,
string $masker = '*',
int $visible = 4
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$phone | string | Phone string. |
$masker | string | Mask string (default: '*'). |
$visible | int | Number of ending bytes to show (default: 4). |
Return:
string - Masked value, or the original if it already fits within the visible count.
Examples:
use Luminova\Utility\Mask;
echo Mask::phone('2025550123'); // ******0123
echo Mask::phone('2025550123', masker: 'x', visible: 2); // xxxxxxxx23card
Strip non-digits, mask the leading digits, and group the result in blocks of four. Use a positive visible count shorter than the digit count. A count of zero or less masks all digits.
public static function card(
string $card,
string $masker = '*',
int $visible = 4
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$card | string | Card number string. |
$masker | string | Mask string (default: '*'). |
$visible | int | Count of ending digits to show (default: 4). |
Return:
string - Masked card display, or the original when its digit count does not exceed the visible count.
Examples:
use Luminova\Utility\Mask;
echo Mask::card('4111 1111 1111 1234'); // **** **** **** 1234
echo Mask::card('4111-1111-1111-1234', masker: 'X', visible: 6); // XXXX XXXX XX11 1234password
Mask the beginning of a password and retain a suffix when the visible count is between one and the byte length minus one. A count at least as large as the password length yields a fully masked string. A count of zero or less also masks the entire value. Avoid this method for redaction.
public static function password(string $password, int $visible = 3): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$password | string | Password string. |
$visible | int | Number of ending bytes requested (default: 3). |
Return:
string - Masked display string, subject to the visible-count behavior above.
Examples:
use Luminova\Utility\Mask;
// Use a fictional value to demonstrate the output.
echo Mask::password('DemoPass123'); // ********123
echo Mask::password('DemoPass123', visible: 11); // ***********