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