Skip to content

Commit 92cfdfb

Browse files
[8.x] Adds mask() for masking part of a string (#39393)
* [8.x] Adds `mask()` for masking part of a string. * Removed multi-byte functions due to no string functions consistency. * Fixed method for PHP 7.3 and PHP 7.4. * Refactored helper for multi-byte strings and custom encoding. * Style fixing. * Update Str.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 85a8549 commit 92cfdfb

File tree

4 files changed

+89
-0
lines changed

4 files changed

+89
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,38 @@ public static function markdown($string, array $options = [])
398398
return (string) $converter->convertToHtml($string);
399399
}
400400

401+
/**
402+
* Masks a portion of a string with a repeated character.
403+
*
404+
* @param string $string
405+
* @param string $character
406+
* @param int $index
407+
* @param int|null $length
408+
* @param string $encoding
409+
* @return string
410+
*/
411+
public static function mask($string, $character, $index, $length = null, $encoding = 'UTF-8')
412+
{
413+
if ($character === '') {
414+
return $string;
415+
}
416+
417+
if (is_null($length) && PHP_MAJOR_VERSION < 8) {
418+
$length = mb_strlen($string, $encoding);
419+
}
420+
421+
$segment = mb_substr($string, $index, $length, $encoding);
422+
423+
if ($segment === '') {
424+
return $string;
425+
}
426+
427+
$start = mb_substr($string, 0, mb_strpos($string, $segment, 0, $encoding), $encoding);
428+
$end = mb_substr($string, mb_strpos($string, $segment, 0, $encoding) + mb_strlen($segment, $encoding));
429+
430+
return $start.str_repeat(mb_substr($character, 0, 1, $encoding), mb_strlen($segment, $encoding)).$end;
431+
}
432+
401433
/**
402434
* Get the string matching the given pattern.
403435
*

src/Illuminate/Support/Stringable.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,20 @@ public function markdown(array $options = [])
342342
return new static(Str::markdown($this->value, $options));
343343
}
344344

345+
/**
346+
* Masks a portion of a string with a repeated character.
347+
*
348+
* @param string $character
349+
* @param int $index
350+
* @param int|null $length
351+
* @param string $encoding
352+
* @return static
353+
*/
354+
public function mask($character, $index, $length = null, $encoding = 'UTF-8')
355+
{
356+
return new static(Str::mask($this->value, $character, $index, $length, $encoding));
357+
}
358+
345359
/**
346360
* Get the string matching the given pattern.
347361
*

tests/Support/SupportStrTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,28 @@ public function testStudly()
451451
$this->assertSame('FooBarBaz', Str::studly('foo-bar_baz'));
452452
}
453453

454+
public function testMask()
455+
{
456+
$this->assertSame('tay*************', Str::mask('[email protected]', '*', 3));
457+
$this->assertSame('******@email.com', Str::mask('[email protected]', '*', 0, 6));
458+
$this->assertSame('tay*************', Str::mask('[email protected]', '*', -13));
459+
$this->assertSame('tay***@email.com', Str::mask('[email protected]', '*', -13, 3));
460+
461+
$this->assertSame('****************', Str::mask('[email protected]', '*', -17));
462+
$this->assertSame('*****[email protected]', Str::mask('[email protected]', '*', -99, 5));
463+
464+
$this->assertSame('[email protected]', Str::mask('[email protected]', '*', 16));
465+
$this->assertSame('[email protected]', Str::mask('[email protected]', '*', 16, 99));
466+
467+
$this->assertSame('[email protected]', Str::mask('[email protected]', '', 3));
468+
469+
$this->assertSame('taysssssssssssss', Str::mask('[email protected]', 'something', 3));
470+
$this->assertSame('taysssssssssssss', Str::mask('[email protected]', Str::of('something'), 3));
471+
472+
$this->assertSame('这是一***', Str::mask('这是一段中文', '*', 3));
473+
$this->assertSame('**一段中文', Str::mask('这是一段中文', '*', 0, 2));
474+
}
475+
454476
public function testMatch()
455477
{
456478
$this->assertSame('bar', Str::match('/bar/', 'foo bar'));

tests/Support/SupportStringableTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,27 @@ public function testMarkdown()
675675
$this->assertEquals("<h1>hello world</h1>\n", $this->stringable('# hello world')->markdown());
676676
}
677677

678+
public function testMask()
679+
{
680+
$this->assertEquals('tay*************', $this->stringable('[email protected]')->mask('*', 3));
681+
$this->assertEquals('******@email.com', $this->stringable('[email protected]')->mask('*', 0, 6));
682+
$this->assertEquals('tay*************', $this->stringable('[email protected]')->mask('*', -13));
683+
$this->assertEquals('tay***@email.com', $this->stringable('[email protected]')->mask('*', -13, 3));
684+
685+
$this->assertEquals('****************', $this->stringable('[email protected]')->mask('*', -17));
686+
$this->assertEquals('*****[email protected]', $this->stringable('[email protected]')->mask('*', -99, 5));
687+
688+
$this->assertEquals('[email protected]', $this->stringable('[email protected]')->mask('*', 16));
689+
$this->assertEquals('[email protected]', $this->stringable('[email protected]')->mask('*', 16, 99));
690+
691+
$this->assertEquals('[email protected]', $this->stringable('[email protected]')->mask('', 3));
692+
693+
$this->assertEquals('taysssssssssssss', $this->stringable('[email protected]')->mask('something', 3));
694+
695+
$this->assertEquals('这是一***', $this->stringable('这是一段中文')->mask('*', 3));
696+
$this->assertEquals('**一段中文', $this->stringable('这是一段中文')->mask('*', 0, 2));
697+
}
698+
678699
public function testRepeat()
679700
{
680701
$this->assertSame('aaaaa', (string) $this->stringable('a')->repeat(5));

0 commit comments

Comments
 (0)