Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function addCharAtInterval() to Str class #52589

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ public static function afterLast($subject, $search)
return substr($subject, $position + strlen($search));
}

/**
* Return the given string with a custom character every X character.
*
* @param string $subject
* @param string $char
* @param int $length
* @return string
*/
public static function addCharAtInterval($subject, $char, $length = 1)
{
return implode($char, str_split($subject, $length));
}

/**
* Transliterate a UTF-8 value to ASCII.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ public function after($search)
return new static(Str::after($this->value, $search));
}

/**
* Return the given string with a custom character every X character.
*
* @param $char
* @param $length
* @return static
*/
public function addCharAtInterval($char, $length = 1)
{
return new static(Str::addCharAtInterval($this->value, $char, $length));
}

/**
* Return the remainder of a string after the last occurrence of a given value.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

class SupportStrTest extends TestCase
{
public function testItAddsACustomCharacterEveryXTimes(): void
{
$this->assertSame('12 34 56 78 90', Str::addCharAtInterval('1234567890', ' ', 2));
$this->assertSame('123-456-789', Str::addCharAtInterval('123456789', '-', 3));
}

public function testStringCanBeLimitedByWords(): void
{
$this->assertSame('Taylor...', Str::words('Taylor Otwell', 1));
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class_basename(static::class),
);
}

public function testItAddsACustomCharacterEveryXTimes()
{
$this->assertSame('12 34 56 78 90', (string) $this->stringable('1234567890')->addCharAtInterval(' ', 2));
$this->assertSame('123-456-789', (string) $this->stringable('123456789')->addCharAtInterval('-', 3));
}

public function testIsAscii()
{
$this->assertTrue($this->stringable('A')->isAscii());
Expand Down