function public static function Unicode::splitDelimiter

8.x-3.x Unicode.php public static Unicode::splitDelimiter($string, $delimiter = '.', $escapable = TRUE)

Splits a string by a specified delimiter, allowing them to be escaped.

Note: this is primarily useful in situations where dot notation is used where the values also contain dots, like in a semantic version string.

Parameters

string $string: The string to split into parts.

string $delimiter: The delimiter used to split the string.

bool $escapable: Flag indicating whether the $delimiter can be escaped using a backward slash ().

Return value

array An array of strings, split where the specified $delimiter was present.

Class

Unicode
Extends \Drupal\Component\Utility\Unicode.

Namespace

Drupal\bootstrap\Utility

Source src/Utility/Unicode.php (line 177)

public static function splitDelimiter($string, $delimiter = '.', $escapable = TRUE) {
  if (!$escapable) {
    return explode($delimiter, $string);
  }

  // Split based on delimiter.
  $parts = preg_split('~\\\\' . preg_quote($delimiter, '~') . '(*SKIP)(*FAIL)|\.~s', $string);

  // Iterate over the parts and remove backslashes from delimiters.
  return array_map(function($string) use ($delimiter) {
    return str_replace("\\$delimiter", $delimiter, $string);
  }, $parts);
}