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

Breadcrumb

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

function Mbstring::mb_str_split

File

vendor/symfony/polyfill-mbstring/Mbstring.php, line 573

Class

Mbstring
Partial mbstring implementation in PHP, iconv based, UTF-8 centric.

Namespace

Symfony\Polyfill\Mbstring

Code

public static function mb_str_split($string, $split_length = 1, $encoding = null) {
    if (null !== $string && !\is_scalar($string) && !(\is_object($string) && method_exists($string, '__toString'))) {
        trigger_error('mb_str_split() expects parameter 1 to be string, ' . \gettype($string) . ' given', \E_USER_WARNING);
        return null;
    }
    if (1 > ($split_length = (int) $split_length)) {
        if (80000 > \PHP_VERSION_ID) {
            trigger_error('The length of each segment must be greater than zero', \E_USER_WARNING);
            return false;
        }
        throw new \ValueError('Argument #2 ($length) must be greater than 0');
    }
    if (null === $encoding) {
        $encoding = mb_internal_encoding();
    }
    if ('UTF-8' === ($encoding = self::getEncoding($encoding))) {
        $rx = '/(';
        while (65535 < $split_length) {
            $rx .= '.{65535}';
            $split_length -= 65535;
        }
        $rx .= '.{' . $split_length . '})/us';
        return preg_split($rx, $string, -1, \PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY);
    }
    $result = [];
    $length = mb_strlen($string, $encoding);
    for ($i = 0; $i < $length; $i += $split_length) {
        $result[] = mb_substr($string, $i, $split_length, $encoding);
    }
    return $result;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal