Tokenizes a string using `$options['separator']`, ignoring any instances of `$options['separator']` that appear between `$options['leftBound']` and `$options['rightBound']`.

Parameters

  • string $data The data to tokenize.
  • array $options Options to use when tokenizing: -`'separator'` _string_: The token to split the data on. -`'leftBound'` _string_: Left scope-enclosing boundary. -`'rightBound'` _string_: Right scope-enclosing boundary.

Returns

array Returns an array of tokens.

Source

						public static function tokenize($data, array $options = array()) {
		$defaults = array('separator' => ',', 'leftBound' => '(', 'rightBound' => ')');
		extract($options + $defaults);

		if (!$data || is_array($data)) {
			return $data;
		}

		$depth = 0;
		$offset = 0;
		$buffer = '';
		$results = array();
		$length = strlen($data);
		$open = false;

		while ($offset <= $length) {
			$tmpOffset = -1;
			$offsets = array(
				strpos($data, $separator, $offset),
				strpos($data, $leftBound, $offset),
				strpos($data, $rightBound, $offset)
			);

			for ($i = 0; $i < 3; $i++) {
				if ($offsets[$i] !== false && ($offsets[$i] < $tmpOffset || $tmpOffset == -1)) {
					$tmpOffset = $offsets[$i];
				}
			}

			if ($tmpOffset === -1) {
				$results[] = $buffer . substr($data, $offset);
				$offset = $length + 1;
				continue;
			}
			$buffer .= substr($data, $offset, ($tmpOffset - $offset));

			if ($data{$tmpOffset} == $separator && $depth == 0) {
				$results[] = $buffer;
				$buffer = '';
			} else {
				$buffer .= $data{$tmpOffset};
			}

			if ($leftBound != $rightBound) {
				if ($data{$tmpOffset} == $leftBound) {
					$depth++;
				}
				if ($data{$tmpOffset} == $rightBound) {
					$depth--;
				}
				$offset = ++$tmpOffset;
				continue;
			}

			if ($data{$tmpOffset} == $leftBound) {
				($open) ? $depth-- : $depth++;
				$open = !$open;
			}
			$offset = ++$tmpOffset;
		}

		if (!$results && $buffer) {
			$results[] = $buffer;
		}
		return $results ? array_map('trim', $results) : array();
	}