Skip to content

[9.x] Add option to ignore case in Str::contains and Str::containsAll #37330

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

Merged
merged 1 commit into from
May 10, 2021
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
16 changes: 14 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,16 @@ public static function camel($value)
*
* @param string $haystack
* @param string|string[] $needles
* @param bool $ignoreCase
* @return bool
*/
public static function contains($haystack, $needles)
public static function contains($haystack, $needles, $ignoreCase = false)
{
if ($ignoreCase) {
$haystack = mb_strtolower($haystack);
$needles = array_map('mb_strtolower', (array) $needles);
}

foreach ((array) $needles as $needle) {
if ($needle !== '' && mb_strpos($haystack, $needle) !== false) {
return true;
Expand All @@ -194,10 +200,16 @@ public static function contains($haystack, $needles)
*
* @param string $haystack
* @param string[] $needles
* @param bool $ignoreCase
* @return bool
*/
public static function containsAll($haystack, array $needles)
public static function containsAll($haystack, array $needles, $ignoreCase = false)
{
if ($ignoreCase) {
$haystack = mb_strtolower($haystack);
$needles = array_map('mb_strtolower', $needles);
}

foreach ($needles as $needle) {
if (! static::contains($haystack, $needle)) {
return false;
Expand Down
53 changes: 40 additions & 13 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,20 @@ public function testStrAfterLast()
$this->assertSame('foo', Str::afterLast('----foo', '---'));
}

public function testStrContains()
/**
* @dataProvider strContainsProvider
*/
public function testStrContains($haystack, $needles, $expected, $ignoreCase = false)
{
$this->assertTrue(Str::contains('taylor', 'ylo'));
$this->assertTrue(Str::contains('taylor', 'taylor'));
$this->assertTrue(Str::contains('taylor', ['ylo']));
$this->assertTrue(Str::contains('taylor', ['xxx', 'ylo']));
$this->assertFalse(Str::contains('taylor', 'xxx'));
$this->assertFalse(Str::contains('taylor', ['xxx']));
$this->assertFalse(Str::contains('taylor', ''));
$this->assertFalse(Str::contains('', ''));
$this->assertEquals($expected, Str::contains($haystack, $needles, $ignoreCase));
}

public function testStrContainsAll()
/**
* @dataProvider strContainsAllProvider
*/
public function testStrContainsAll($haystack, $needles, $expected, $ignoreCase = false)
{
$this->assertTrue(Str::containsAll('taylor otwell', ['taylor', 'otwell']));
$this->assertTrue(Str::containsAll('taylor otwell', ['taylor']));
$this->assertFalse(Str::containsAll('taylor otwell', ['taylor', 'xxx']));
$this->assertEquals($expected, Str::containsAll($haystack, $needles, $ignoreCase));
}

public function testParseCallback()
Expand Down Expand Up @@ -545,6 +542,36 @@ public function invalidUuidList()
];
}

public function strContainsProvider()
{
return [
['Taylor', 'ylo', true, true],
['Taylor', 'ylo', true, false],
['Taylor', 'taylor', true, true],
['Taylor', 'taylor', false, false],
['Taylor', ['ylo'], true, true],
['Taylor', ['ylo'], true, false],
['Taylor', ['xxx', 'ylo'], true, true],
['Taylor', ['xxx', 'ylo'], true, false],
['Taylor', 'xxx', false],
['Taylor', ['xxx'], false],
['Taylor', '', false],
['', '', false],
];
}

public function strContainsAllProvider()
{
return [
['Taylor Otwell', ['taylor', 'otwell'], false, false],
['Taylor Otwell', ['taylor', 'otwell'], true, true],
['Taylor Otwell', ['taylor'], false, false],
['Taylor Otwell', ['taylor'], true, true],
['Taylor Otwell', ['taylor', 'xxx'], false, false],
['Taylor Otwell', ['taylor', 'xxx'], false, true],
];
}

public function testMarkdown()
{
$this->assertSame("<p><em>hello world</em></p>\n", Str::markdown('*hello world*'));
Expand Down