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

[7.x] Add substrCount to Stringable and Str #32393

Merged
merged 3 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
18 changes: 18 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,24 @@ public static function substr($string, $start, $length = null)
return mb_substr($string, $start, $length, 'UTF-8');
}

/**
* Returns the number of substring occurrences.
*
* @param string $haystack
* @param string $needle
* @param int $offset
* @param int|null $length
* @return int
*/
public static function substrCount($haystack, $needle, $offset = 0, $length = null)
{
if ($length) {
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
return substr_count($haystack, $needle, $offset, $length);
} else {
return substr_count($haystack, $needle, $offset);
}
}

/**
* Make a string's first character uppercase.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,19 @@ public function substr($start, $length = null)
return new static(Str::substr($this->value, $start, $length));
}

/**
* Returns the number of substring occurrences.
*
* @param string $needle
* @param int|null $offset
* @param int|null $length
* @return int
*/
public function substrCount($needle, $offset = null, $length = null)
{
return Str::substrCount($this->value, $needle, $offset, $length);
}

/**
* Trim the string of the given characters.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,20 @@ public function testSubstr()
$this->assertEmpty(Str::substr('Б', 2));
}

public function testSubstrCount()
{
$this->assertSame(3, Str::substrCount('laravelPHPFramework', 'a'));
$this->assertSame(0, Str::substrCount('laravelPHPFramework', 'z'));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'l', 2));
$this->assertSame(0, Str::substrCount('laravelPHPFramework', 'z', 2));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'k', -1));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'k', -1));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', 1, 2));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', 1, 2));
$this->assertSame(3, Str::substrCount('laravelPHPFramework', 'a', 1, -2));
$this->assertSame(1, Str::substrCount('laravelPHPFramework', 'a', -10, -3));
}

public function testUcfirst()
{
$this->assertSame('Laravel', Str::ucfirst('laravel'));
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,18 @@ public function testSubstr()
$this->assertSame('Ё', (string) $this->stringable('БГДЖИЛЁ')->substr(-1, 1));
$this->assertSame('', (string) $this->stringable('Б')->substr(2));
}

public function testSubstrCount()
{
$this->assertSame(3, $this->stringable('laravelPHPFramework')->substrCount('a'));
$this->assertSame(0, $this->stringable('laravelPHPFramework')->substrCount('z'));
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('l', 2));
$this->assertSame(0, $this->stringable('laravelPHPFramework')->substrCount('z', 2));
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('k', -1));
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('k', -1));
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', 1, 2));
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', 1, 2));
$this->assertSame(3, $this->stringable('laravelPHPFramework')->substrCount('a', 1, -2));
$this->assertSame(1, $this->stringable('laravelPHPFramework')->substrCount('a', -10, -3));
}
}