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

Breadcrumb

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

function StringInput::tokenize

Tokenizes a string.

Return value

list<string>

Throws

InvalidArgumentException When unable to parse input (should never happen)

1 call to StringInput::tokenize()
StringInput::__construct in vendor/symfony/console/Input/StringInput.php

File

vendor/symfony/console/Input/StringInput.php, line 47

Class

StringInput
StringInput represents an input provided as a string.

Namespace

Symfony\Component\Console\Input

Code

private function tokenize(string $input) : array {
    $tokens = [];
    $length = \strlen($input);
    $cursor = 0;
    $token = null;
    while ($cursor < $length) {
        if ('\\' === $input[$cursor]) {
            $token .= $input[++$cursor] ?? '';
            ++$cursor;
            continue;
        }
        if (preg_match('/\\s+/A', $input, $match, 0, $cursor)) {
            if (null !== $token) {
                $tokens[] = $token;
                $token = null;
            }
        }
        elseif (preg_match('/([^="\'\\s]+?)(=?)(' . self::REGEX_QUOTED_STRING . '+)/A', $input, $match, 0, $cursor)) {
            $token .= $match[1] . $match[2] . stripcslashes(str_replace([
                '"\'',
                '\'"',
                '\'\'',
                '""',
            ], '', substr($match[3], 1, -1)));
        }
        elseif (preg_match('/' . self::REGEX_QUOTED_STRING . '/A', $input, $match, 0, $cursor)) {
            $token .= stripcslashes(substr($match[0], 1, -1));
        }
        elseif (preg_match('/' . self::REGEX_UNQUOTED_STRING . '/A', $input, $match, 0, $cursor)) {
            $token .= $match[1];
        }
        else {
            // should never happen
            throw new InvalidArgumentException(\sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10)));
        }
        $cursor += \strlen($match[0]);
    }
    if (null !== $token) {
        $tokens[] = $token;
    }
    return $tokens;
}

API Navigation

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