You are here

public function AutocompleteHelper::splitKeys in Search API Autocomplete 8

Splits a string with search keywords into two parts.

The first part consists of all words the user has typed completely, the second one contains the beginning of the last, possibly incomplete word.

Parameters

string $keys: The passed in keys.

Return value

string[] An array with $keys split into exactly two parts, both of which may be empty.

Overrides AutocompleteHelperInterface::splitKeys

File

src/Utility/AutocompleteHelper.php, line 36

Class

AutocompleteHelper
Provides helper methods for creating autocomplete suggestions.

Namespace

Drupal\search_api_autocomplete\Utility

Code

public function splitKeys($keys) {
  $keys = ltrim($keys);

  // If there is whitespace or a quote on the right, all words have been
  // completed.
  if (rtrim($keys, " \"") != $keys) {
    return [
      rtrim($keys, ' '),
      '',
    ];
  }
  if (preg_match('/^(.*?)\\s*"?([\\S]*)$/', $keys, $m)) {
    return [
      $m[1],
      $m[2],
    ];
  }
  return [
    '',
    $keys,
  ];
}