lithium\util\String::tokenize PHP Méthode

tokenize() public static méthode

Tokenizes a string using $options['separator'], ignoring any instances of $options['separator'] that appear between $options['leftBound'] and $options['rightBound'].
public static tokenize ( string $data, array $options = [] ) : array
$data string The data to tokenize.
$options array 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.
Résultat array Returns an array of tokens.
    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();
    }

Usage Example

 /**
  * testTokenize method
  *
  * @return void
  */
 public function testTokenize()
 {
     $result = String::tokenize('A,(short,boring test)');
     $expected = array('A', '(short,boring test)');
     $this->assertEqual($expected, $result);
     $result = String::tokenize('A,(short,more interesting( test)');
     $expected = array('A', '(short,more interesting( test)');
     $this->assertEqual($expected, $result);
     $result = String::tokenize('A,(short,very interesting( test))');
     $expected = array('A', '(short,very interesting( test))');
     $this->assertEqual($expected, $result);
     $result = String::tokenize('"single tag"', array('separator' => ' ', 'leftBound' => '"', 'rightBound' => '"'));
     $expected = array('"single tag"');
     $this->assertEqual($expected, $result);
     $result = String::tokenize('tagA "single tag" tagB', array('separator' => ' ', 'leftBound' => '"', 'rightBound' => '"'));
     $expected = array('tagA', '"single tag"', 'tagB');
     $this->assertEqual($expected, $result);
     $result = String::tokenize(array());
     $expected = array();
     $this->assertEqual($expected, $result);
     $result = String::tokenize(null);
     $this->assertNull($result);
 }