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

Breadcrumb

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

function UnicodeString::split

Overrides AbstractString::split

File

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

Class

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

Namespace

Symfony\Component\String

Code

public function split(string $delimiter, ?int $limit = null, ?int $flags = null) : array {
    if (1 > ($limit ??= 2147483647)) {
        throw new InvalidArgumentException('Split limit must be a positive integer.');
    }
    if ('' === $delimiter) {
        throw new InvalidArgumentException('Split delimiter is empty.');
    }
    if (null !== $flags) {
        return parent::split($delimiter . 'u', $limit, $flags);
    }
    normalizer_is_normalized($delimiter) ?: ($delimiter = normalizer_normalize($delimiter));
    if (false === $delimiter) {
        throw new InvalidArgumentException('Split delimiter is not a valid UTF-8 string.');
    }
    $str = clone $this;
    $tail = $this->string;
    $chunks = [];
    $indexOf = $this->ignoreCase ? 'grapheme_stripos' : 'grapheme_strpos';
    while (1 < $limit && false !== ($i = $indexOf($tail, $delimiter))) {
        $str->string = grapheme_substr($tail, 0, $i);
        $chunks[] = clone $str;
        $tail = substr($tail, \strlen($str->string) + \strlen($delimiter));
        --$limit;
    }
    $str->string = $tail;
    $chunks[] = clone $str;
    return $chunks;
}
RSS feed
Powered by Drupal