diff --git a/src/Liquid/StandardFilters.php b/src/Liquid/StandardFilters.php index 086b517f..2e7d62e7 100644 --- a/src/Liquid/StandardFilters.php +++ b/src/Liquid/StandardFilters.php @@ -410,13 +410,13 @@ public static function prepend($input, $string) * Remove a substring * * @param string $input - * @param string $string + * @param string|null $string * * @return string */ public static function remove($input, $string) { - return str_replace($string, '', $input); + return self::replace($input, $string); } @@ -441,15 +441,18 @@ public static function remove_first($input, $string) /** * Replace occurrences of a string with another * - * @param string $input - * @param string $string - * @param string $replacement + * @param string|null $input + * @param string|null $string + * @param string|null $replacement * * @return string */ public static function replace($input, $string, $replacement = '') { - return str_replace($string, $replacement, $input); + if ($string === null) { + return $input; + } + return str_replace($string, (string)$replacement, (string)$input); } diff --git a/tests/Liquid/OutputTest.php b/tests/Liquid/OutputTest.php index de035b73..f10519da 100644 --- a/tests/Liquid/OutputTest.php +++ b/tests/Liquid/OutputTest.php @@ -224,4 +224,18 @@ public function testFilterArrayNull() $this->assertTemplateResult($expected, $text, []); } + + public function testRemoveWithNull(): void + { + $text = ' {{ best_cars | remove: brand }} '; + + $this->assertTemplateResult(' bmw ', $text, [...$this->assigns, 'brand' => null]); + } + + public function testReplaceWithNull() + { + $text = ' {{ best_cars | replace: brand }} '; + + $this->assertTemplateResult(' bmw ', $text, [...$this->assigns, 'brand' => null]); + } }