Oihana PHP

mask.php

Table of Contents

Functions

mask()  : string
Masks the middle part of a string, keeping a few grapheme clusters visible at each end.

Functions

mask()

Masks the middle part of a string, keeping a few grapheme clusters visible at each end.

mask(string $source[, int $visibleStart = 0 ][, int $visibleEnd = 4 ][, string $char = '*' ]) : string

Useful for hiding sensitive data (emails, tokens, card numbers). The first $visibleStart and last $visibleEnd grapheme clusters are kept, every grapheme in between is replaced by $char. When the visible regions cover (or overlap) the whole string, there is nothing to hide and the original string is returned unchanged.

Parameters
$source : string

The input string.

$visibleStart : int = 0

The number of leading grapheme clusters to keep visible. Default 0.

$visibleEnd : int = 4

The number of trailing grapheme clusters to keep visible. Default 4.

$char : string = '*'

The masking character. Default *.

Tags
example
use function oihana\core\strings\mask;

echo mask( '4242424242424242' )            ; // "************4242"
echo mask( 'secret' , 1 , 1 )              ; // "s****t"
echo mask( 'john@doe.com' , 2 , 4 , '•' )  ; // "jo••••••.com"
echo mask( 'abc' , 2 , 2 )                 ; // "abc" (regions overlap, returned as-is)
author

Marc Alcaraz (ekameleon)

since
1.0.9
Return values
string

The masked string.

On this page

Search results