You are here

public static function Tags::explode in Service Container 7.2

Same name and namespace in other branches
  1. 7 lib/Drupal/Component/Utility/Tags.php \Drupal\Component\Utility\Tags::explode()

Explodes a string of tags into an array.

Parameters

string $tags: A string to explode.

Return value

array An array of tags.

File

lib/Drupal/Component/Utility/Tags.php, line 26
Contains \Drupal\Component\Utility\Tags.

Class

Tags
Defines a class that can explode and implode tags.

Namespace

Drupal\Component\Utility

Code

public static function explode($tags) {

  // This regexp allows the following types of user input:
  // this, "somecompany, llc", "and ""this"" w,o.rks", foo bar
  $regexp = '%(?:^|,\\ *)("(?>[^"]*)(?>""[^"]* )*"|(?: [^",]*))%x';
  preg_match_all($regexp, $tags, $matches);
  $typed_tags = array_unique($matches[1]);
  $tags = array();
  foreach ($typed_tags as $tag) {

    // If a user has escaped a term (to demonstrate that it is a group,
    // or includes a comma or quote character), we remove the escape
    // formatting so to save the term into the database as the user intends.
    $tag = trim(str_replace('""', '"', preg_replace('/^"(.*)"$/', '\\1', $tag)));
    if ($tag != "") {
      $tags[] = $tag;
    }
  }
  return $tags;
}