Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AbstractUnicodeString.php

function AbstractUnicodeString::replaceMatches

Overrides AbstractString::replaceMatches

2 calls to AbstractUnicodeString::replaceMatches()
UnicodeString::replaceMatches in vendor/symfony/string/UnicodeString.php
UnicodeString::replaceMatches in vendor/symfony/string/UnicodeString.php
1 method overrides AbstractUnicodeString::replaceMatches()
UnicodeString::replaceMatches in vendor/symfony/string/UnicodeString.php

File

vendor/symfony/string/AbstractUnicodeString.php, line 307

Class

AbstractUnicodeString
Represents a string of abstract Unicode characters.

Namespace

Symfony\Component\String

Code

public function replaceMatches(string $fromRegexp, string|callable $to) : static {
    if ($this->ignoreCase) {
        $fromRegexp .= 'i';
    }
    if (\is_array($to) || $to instanceof \Closure) {
        $replace = 'preg_replace_callback';
        $to = static function (array $m) use ($to) : string {
            $to = $to($m);
            if ('' !== $to && (!\is_string($to) || !preg_match('//u', $to))) {
                throw new InvalidArgumentException('Replace callback must return a valid UTF-8 string.');
            }
            return $to;
        };
    }
    elseif ('' !== $to && !preg_match('//u', $to)) {
        throw new InvalidArgumentException('Invalid UTF-8 string.');
    }
    else {
        $replace = 'preg_replace';
    }
    set_error_handler(static fn($t, $m) => throw new InvalidArgumentException($m));
    try {
        if (null === ($string = $replace($fromRegexp . 'u', $to, $this->string))) {
            $lastError = preg_last_error();
            foreach (get_defined_constants(true)['pcre'] as $k => $v) {
                if ($lastError === $v && str_ends_with($k, '_ERROR')) {
                    throw new RuntimeException('Matching failed with ' . $k . '.');
                }
            }
            throw new RuntimeException('Matching failed with unknown error code.');
        }
    } finally {
        restore_error_handler();
    }
    $str = clone $this;
    $str->string = $string;
    return $str;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal