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

Breadcrumb

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

function UnicodeString::replace

Overrides AbstractString::replace

File

vendor/symfony/string/UnicodeString.php, line 235

Class

UnicodeString
Represents a string of Unicode grapheme clusters encoded as UTF-8.

Namespace

Symfony\Component\String

Code

public function replace(string $from, string $to) : static {
    $str = clone $this;
    normalizer_is_normalized($from) ?: ($from = normalizer_normalize($from));
    if ('' !== $from && false !== $from) {
        $tail = $str->string;
        $result = '';
        $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
        while ('' !== $tail && false !== ($i = $indexOf($tail, $from))) {
            $slice = grapheme_substr($tail, 0, $i);
            $result .= $slice . $to;
            $tail = substr($tail, \strlen($slice) + \strlen($from));
        }
        $str->string = $result . $tail;
        if (normalizer_is_normalized($str->string)) {
            return $str;
        }
        if (false === ($string = normalizer_normalize($str->string))) {
            throw new InvalidArgumentException('Invalid UTF-8 string.');
        }
        $str->string = $string;
    }
    return $str;
}
RSS feed
Powered by Drupal