You are here

public function TwigExtension::pregReplaceFilter in Twig Tweak 8

Same name and namespace in other branches
  1. 8.2 src/TwigExtension.php \Drupal\twig_tweak\TwigExtension::pregReplaceFilter()

Performs a regular expression search and replace.

Parameters

string $text: The text to search and replace.

string $pattern: The pattern to search for.

string $replacement: The string to replace.

Return value

string The new text if matches are found, otherwise unchanged text.

File

src/TwigExtension.php, line 419

Class

TwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public function pregReplaceFilter($text, $pattern, $replacement) {

  // BC layer. Before version 8.x-1.8 the pattern was without delimiters.
  // @todo Remove this in Drupal 9.
  if (strpos($pattern, '/') !== 0) {
    return preg_replace("/{$pattern}/", $replacement, $text);
  }
  return preg_replace($pattern, $replacement, $text);
}