Skip to content

Commit 922713d

Browse files
authored
add missing docs for maskRegex and maskPartialRegex (#8328)
1 parent 67d95db commit 922713d

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

docs/en/appendices/5-4-migration-guide.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ events are now registered while each plugin is bootstrapped.
241241
You can set `Security.encryptWithRawKey` to enable this behavior. See [here](https://github.com/cakephp/cakephp/pull/19325) for more details.
242242
- Added `Text::mask()` method which masks a portion of a string with a repeated character. See [Text Masking](../core-libraries/text.md#text-masking) for more details.
243243
- Added `Text::maskValue()` method which masks all occurrences of given substrings within a string using a repeated character. See [Text Masking](../core-libraries/text.md#text-masking) for more details.
244+
- Added `Text::maskRegex()` and `Text::maskPartialRegex()` methods for masking
245+
regular expression matches. See [Text Masking](../core-libraries/text.md#text-masking)
246+
for more details.
244247

245248
### View
246249

docs/en/core-libraries/text.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,4 +501,58 @@ echo Text::maskValue('4111111111111234', ['411', '112'], '*');
501501
Output:
502502

503503
***11111111***34
504+
505+
### Text::maskRegex()
506+
507+
`method` Cake\\Utility\\Text::**maskRegex**(string $string, array|string $patterns, string $maskCharacter = '*'): string
508+
509+
Masks all occurrences of given regular expression pattern(s) within a string
510+
using a repeated character. Each match is replaced by a sequence of the masking
511+
character with the same length as the match.
512+
513+
The `$patterns` argument accepts a single regular expression or an array of
514+
regular expressions. Empty patterns are ignored. `$maskCharacter` must be a
515+
single character.
516+
517+
```php
518+
// Called as TextHelper
519+
echo $this->Text->maskRegex('my pin is 1234', '/\d+/', '*');
520+
521+
// Called as Text
522+
use Cake\Utility\Text;
523+
524+
echo Text::maskRegex('token: abc123', '/[a-z0-9]+/', 'x');
525+
```
526+
527+
Output:
528+
529+
my pin is ****
530+
xxxxx: xxxxxx
531+
532+
### Text::maskPartialRegex()
533+
534+
`method` Cake\\Utility\\Text::**maskPartialRegex**(string $string, array|string $patterns, int $showLeading = 0, int $showTrailing = 0, string $maskCharacter = '*'): string
535+
536+
Masks all occurrences of given regular expression pattern(s) within a string,
537+
while preserving a number of leading and trailing characters from each match.
538+
539+
The `$showLeading` and `$showTrailing` arguments control how many characters at
540+
the start and end of each match are left unmasked. If their combined value is
541+
greater than or equal to the match length, the match is left unchanged. These
542+
values must be non-negative, and `$maskCharacter` must be a single character.
543+
544+
```php
545+
// Called as TextHelper
546+
echo $this->Text->maskPartialRegex('card: 4242424242424242', '/\d{16}/', 0, 4);
547+
548+
// Called as Text
549+
use Cake\Utility\Text;
550+
551+
echo Text::maskPartialRegex('Secret Codeword', '/\b\w+\b/', 1, 1);
552+
```
553+
554+
Output:
555+
556+
card: ************4242
557+
S****t C******d
504558
<!-- end-text -->

0 commit comments

Comments
 (0)