lithium\util\String::tokenize()
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
arrayReturns an array of tokens.
Source
public static function tokenize($data, array $options = array()) {
$options += array('separator' => ',', 'leftBound' => '(', 'rightBound' => ')');
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, $options['separator'], $offset),
strpos($data, $options['leftBound'], $offset),
strpos($data, $options['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] === $options['separator'] && $depth === 0) {
$results[] = $buffer;
$buffer = '';
} else {
$buffer .= $data{$tmpOffset};
}
if ($options['leftBound'] !== $options['rightBound']) {
if ($data[$tmpOffset] === $options['leftBound']) {
$depth++;
}
if ($data[$tmpOffset] === $options['rightBound']) {
$depth--;
}
$offset = ++$tmpOffset;
continue;
}
if ($data[$tmpOffset] === $options['leftBound']) {
($open) ? $depth-- : $depth++;
$open = !$open;
}
$offset = ++$tmpOffset;
}
if (!$results && $buffer) {
$results[] = $buffer;
}
return $results ? array_map('trim', $results) : array();
}