Skip to content

Commit

Permalink
Strings::replace: default replacement is empty string [Closes #241]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 3, 2020
1 parent d0427c1 commit eab9924
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Utils/Strings.php
Expand Up @@ -514,15 +514,15 @@ public static function matchAll(string $subject, string $pattern, int $flags = 0
* @param string|array $pattern
* @param string|callable $replacement
*/
public static function replace(string $subject, $pattern, $replacement = null, int $limit = -1): string
public static function replace(string $subject, $pattern, $replacement = '', int $limit = -1): string
{
if (is_object($replacement) || is_array($replacement)) {
if (!is_callable($replacement, false, $textual)) {
throw new Nette\InvalidStateException("Callback '$textual' is not callable.");
}
return self::pcre('preg_replace_callback', [$pattern, $replacement, $subject, $limit]);

} elseif ($replacement === null && is_array($pattern)) {
} elseif (is_array($pattern) && is_string(key($pattern))) {
$replacement = array_values($pattern);
$pattern = array_keys($pattern);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Utils/Strings.replace().phpt
Expand Up @@ -32,3 +32,4 @@ Assert::same('#@ @@@#d!', Strings::replace('hello world!', [
'#([e-l])+#' => '#',
'#[o-w]#' => '@',
]));
Assert::same(' !', Strings::replace('hello world!', '#\w#'));

0 comments on commit eab9924

Please sign in to comment.