Skip to content
Open
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
47 changes: 45 additions & 2 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -1286,7 +1286,50 @@ public static function replace($search, $replace, $subject, $caseSensitive = tru

return $caseSensitive
? str_replace($search, $replace, $subject)
: str_ireplace($search, $replace, $subject);
: static::replaceIgnoreCase($search, $replace, $subject);
}

/**
* Replace the given value in the given string regardless of case.
*
* @param array|string $search
* @param array|string $replace
* @param array|string $subject
Comment on lines +1295 to +1297

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could probably narrow this down to

Suggested change
* @param array|string $search
* @param array|string $replace
* @param array|string $subject
* @param string|string[] $search
* @param string|string[] $replace
* @param string|string[] $subject

* @return string|string[]
*/
protected static function replaceIgnoreCase($search, $replace, $subject)
{
$searches = is_array($search) ? array_values($search) : [$search];

if (static::isAscii(implode('', $searches))) {
return str_ireplace($search, $replace, $subject);
}

foreach ((array) $subject as $value) {
if (! preg_match('//u', (string) $value)) {
return str_ireplace($search, $replace, $subject);
}
}

$replacements = is_array($replace)
? array_values($replace)
: array_fill(0, count($searches), $replace);

foreach ($searches as $index => $term) {
if ((string) $term === '') {
continue;
}

$replacement = (string) ($replacements[$index] ?? '');

$subject = preg_replace_callback(
'/'.preg_quote((string) $term, '/').'/iu',
fn () => $replacement,
$subject
);
}

return $subject;
}

/**
Expand Down Expand Up @@ -1419,7 +1462,7 @@ public static function remove($search, $subject, $caseSensitive = true)

return $caseSensitive
? str_replace($search, '', $subject)
: str_ireplace($search, '', $subject);
: static::replaceIgnoreCase($search, '', $subject);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,11 @@ public function testReplace()
$this->assertSame('foo/bar/baz', Str::replace(' ', '/', 'foo bar baz'));
$this->assertSame('foo bar baz', Str::replace(['?1', '?2', '?3'], ['foo', 'bar', 'baz'], '?1 ?2 ?3'));
$this->assertSame(['foo', 'bar', 'baz'], Str::replace(collect(['?1', '?2', '?3']), collect(['foo', 'bar', 'baz']), collect(['?1', '?2', '?3'])));

$this->assertSame('Xltý kôň', Str::replace('ž', 'X', 'Žltý kôň', false));
$this->assertSame('žltý pes', Str::replace('KÔŇ', 'pes', 'žltý kôň', false));
$this->assertSame('Xltý pes', Str::replace(['ž', 'KÔŇ'], ['X', 'pes'], 'Žltý kôň', false));
$this->assertSame(['Xltý', 'kôň'], Str::replace('ž', 'X', ['Žltý', 'kôň'], false));
}

public function testReplaceArray()
Expand Down Expand Up @@ -1026,6 +1031,9 @@ public function testRemove()
$this->assertSame('Fooar', Str::remove(['f', 'b'], 'Foobar'));
$this->assertSame('ooar', Str::remove(['f', 'b'], 'Foobar', false));
$this->assertSame('Foobar', Str::remove(['f', '|'], 'Foo|bar'));

$this->assertSame('ltý', Str::remove('ž', 'Žltý', false));
$this->assertSame('žltý ', Str::remove('KÔŇ', 'žltý kôň', false));
}

public function testReverse()
Expand Down
Loading