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\StringCode
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;
}