Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,38 @@ public static function markdown($string, array $options = [])
return (string) $converter->convertToHtml($string);
}

/**
* Masks a portion of a string with a repeated character.
*
* @param string $string
* @param string $character
* @param int $index
* @param int|null $length
* @param string $encoding
* @return string
*/
public static function mask($string, $character, $index, $length = null, $encoding = 'UTF-8')
{
if ($character === '') {
return $string;
}

if (is_null($length) && PHP_MAJOR_VERSION < 8) {
$length = mb_strlen($string, $encoding);
}

$segment = mb_substr($string, $index, $length, $encoding);

if ($segment === '') {
return $string;
}

$start = mb_substr($string, 0, mb_strpos($string, $segment, 0, $encoding), $encoding);
$end = mb_substr($string, mb_strpos($string, $segment, 0, $encoding) + mb_strlen($segment, $encoding));

return $start.str_repeat(mb_substr($character, 0, 1, $encoding), mb_strlen($segment, $encoding)).$end;
}

/**
* Get the string matching the given pattern.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,20 @@ public function markdown(array $options = [])
return new static(Str::markdown($this->value, $options));
}

/**
* Masks a portion of a string with a repeated character.
*
* @param string $character
* @param int $index
* @param int|null $length
* @param string $encoding
* @return static
*/
public function mask($character, $index, $length = null, $encoding = 'UTF-8')
{
return new static(Str::mask($this->value, $character, $index, $length, $encoding));
}

/**
* Get the string matching the given pattern.
*
Expand Down
22 changes: 22 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,28 @@ public function testStudly()
$this->assertSame('FooBarBaz', Str::studly('foo-bar_baz'));
}

public function testMask()
{
$this->assertSame('tay*************', Str::mask('taylor@email.com', '*', 3));
$this->assertSame('******@email.com', Str::mask('taylor@email.com', '*', 0, 6));
$this->assertSame('tay*************', Str::mask('taylor@email.com', '*', -13));
$this->assertSame('tay***@email.com', Str::mask('taylor@email.com', '*', -13, 3));

$this->assertSame('****************', Str::mask('taylor@email.com', '*', -17));
$this->assertSame('*****r@email.com', Str::mask('taylor@email.com', '*', -99, 5));

$this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '*', 16));
$this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '*', 16, 99));

$this->assertSame('taylor@email.com', Str::mask('taylor@email.com', '', 3));

$this->assertSame('taysssssssssssss', Str::mask('taylor@email.com', 'something', 3));
$this->assertSame('taysssssssssssss', Str::mask('taylor@email.com', Str::of('something'), 3));

$this->assertSame('这是一***', Str::mask('这是一段中文', '*', 3));
$this->assertSame('**一段中文', Str::mask('这是一段中文', '*', 0, 2));
}

public function testMatch()
{
$this->assertSame('bar', Str::match('/bar/', 'foo bar'));
Expand Down
21 changes: 21 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,27 @@ public function testMarkdown()
$this->assertEquals("<h1>hello world</h1>\n", $this->stringable('# hello world')->markdown());
}

public function testMask()
{
$this->assertEquals('tay*************', $this->stringable('taylor@email.com')->mask('*', 3));
$this->assertEquals('******@email.com', $this->stringable('taylor@email.com')->mask('*', 0, 6));
$this->assertEquals('tay*************', $this->stringable('taylor@email.com')->mask('*', -13));
$this->assertEquals('tay***@email.com', $this->stringable('taylor@email.com')->mask('*', -13, 3));

$this->assertEquals('****************', $this->stringable('taylor@email.com')->mask('*', -17));
$this->assertEquals('*****r@email.com', $this->stringable('taylor@email.com')->mask('*', -99, 5));

$this->assertEquals('taylor@email.com', $this->stringable('taylor@email.com')->mask('*', 16));
$this->assertEquals('taylor@email.com', $this->stringable('taylor@email.com')->mask('*', 16, 99));

$this->assertEquals('taylor@email.com', $this->stringable('taylor@email.com')->mask('', 3));

$this->assertEquals('taysssssssssssss', $this->stringable('taylor@email.com')->mask('something', 3));

$this->assertEquals('这是一***', $this->stringable('这是一段中文')->mask('*', 3));
$this->assertEquals('**一段中文', $this->stringable('这是一段中文')->mask('*', 0, 2));
}

public function testRepeat()
{
$this->assertSame('aaaaa', (string) $this->stringable('a')->repeat(5));
Expand Down