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

Breadcrumb

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

function ByteString::replaceMatches

Overrides AbstractString::replaceMatches

File

vendor/symfony/string/ByteString.php, line 300

Class

ByteString
Represents a binary-safe string of bytes.

Namespace

Symfony\Component\String

Code

public function replaceMatches(string $fromRegexp, string|callable $to) : static {
    if ($this->ignoreCase) {
        $fromRegexp .= 'i';
    }
    $replace = \is_array($to) || $to instanceof \Closure ? 'preg_replace_callback' : 'preg_replace';
    set_error_handler(static fn($t, $m) => throw new InvalidArgumentException($m));
    try {
        if (null === ($string = $replace($fromRegexp, $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