src/text/tokenizer/TextTokenizer.php
public function match($regexp, &$matches = array(), $flags = 0)
    {
        // we do not like empty strings
        if (strlen($regexp) == 0) {
            return false;
        }

        $ret = false;
        $explicitRegexp = strlen($regexp) > 0 && $regexp[0] == "/";
        $substr = substr($this->string, $this->offset);

        if (!$explicitRegexp) {
            $caseSensitive  = TextTokenizer::CASE_SENSITIVE
                & ($this->_flags | $flags);
            $searchAnywhere = TextTokenizer::SEARCH_ANYWHERE
                & ($this->_flags | $flags);
            $modifiers = "us" . ($caseSensitive? "" : "i");
            $regexp = $searchAnywhere
                ? "/($regexp)/$modifiers"
                : "/^\s*($regexp)/$modifiers";
        }

        if (preg_match($regexp, $substr, $matches, PREG_OFFSET_CAPTURE)) {
            $offsetCapture = TextTokenizer::OFFSET_CAPTURE
                              & ($this->_flags | $flags);
            $str = $matches[0][0];
            $offset = $matches[0][1] + strlen($str);

            if ($offsetCapture) {
                // fixes offsets
                foreach ($matches as $i => $match) {
                    $matches[$i][1] += $this->offset;
                }
            } else {
                // ignores offsets
                foreach ($matches as $i => $match) {
                    $matches[$i] = $matches[$i][0];
                }
            }

            if (!ctype_alnum($substr[$offset - 1])
                || $offset == strlen($substr)
                || !ctype_alnum($substr[$offset])
            ) {
                $this->offset += $offset;
                $ret = array(ltrim($str));
            }
        }

        return $ret;
    }
public function match($regexp, &$matches = array(), $flags = 0)
    {
        // we do not like empty strings
        if (strlen($regexp) == 0) {
            return false;
        }

        $ret = false;
        $explicitRegexp = strlen($regexp) > 0 && $regexp[0] == "/";
        $substr = substr($this->string, $this->offset);

        if (!$explicitRegexp) {
            $caseSensitive  = TextTokenizer::CASE_SENSITIVE
                & ($this->_flags | $flags);
            $searchAnywhere = TextTokenizer::SEARCH_ANYWHERE
                & ($this->_flags | $flags);
            $modifiers = "us" . ($caseSensitive? "" : "i");
            $regexp = $searchAnywhere
                ? "/($regexp)/$modifiers"
                : "/^\s*($regexp)/$modifiers";
        }

        if (preg_match($regexp, $substr, $matches, PREG_OFFSET_CAPTURE)) {
            $offsetCapture = TextTokenizer::OFFSET_CAPTURE
                              & ($this->_flags | $flags);
            $str = $matches[0][0];
            $offset = $matches[0][1] + strlen($str);

            if ($offsetCapture) {
                // fixes offsets
                foreach ($matches as $i => $match) {
                    $matches[$i][1] += $this->offset;
                }
            } else {
                // ignores offsets
                foreach ($matches as $i => $match) {
                    $matches[$i] = $matches[$i][0];
                }
            }

            if (!ctype_alnum($substr[$offset - 1])
                || $offset == strlen($substr)
                || !ctype_alnum($substr[$offset])
            ) {
                $this->offset += $offset;
                $ret = array(ltrim($str));
            }
        }

        return $ret;
    }
public function match ( $ regexp , & $ matches = array ( ) , $ flags = 0 ) { // we do not like empty strings if ( strlen ( $ regexp ) == 0 ) { return false ; } $ ret = false ; $ explicitRegexp = strlen ( $ regexp ) > 0 && $ regexp [ 0 ] == "/" ; $ substr = substr ( $ this -> string , $ this -> offset ) ; if ( ! $ explicitRegexp ) { $ caseSensitive = TextTokenizer :: CASE_SENSITIVE & ( $ this -> _flags | $ flags ) ; $ searchAnywhere = TextTokenizer :: SEARCH_ANYWHERE & ( $ this -> _flags | $ flags ) ; $ modifiers = "us" . ( $ caseSensitive ? "" : "i" ) ; $ regexp = $ searchAnywhere ? "/($regexp)/$modifiers" : "/^\s*($regexp)/$modifiers" ; } if ( preg_match ( $ regexp , $ substr , $ matches , PREG_OFFSET_CAPTURE ) ) { $ offsetCapture = TextTokenizer :: OFFSET_CAPTURE & ( $ this -> _flags | $ flags ) ; $ str = $ matches [ 0 ] [ 0 ] ; $ offset = $ matches [ 0 ] [ 1 ] + strlen ( $ str ) ; if ( $ offsetCapture ) { // fixes offsets foreach ( $ matches as $ i => $ match ) { $ matches [ $ i ] [ 1 ] += $ this -> offset ; } } else { // ignores offsets foreach ( $ matches as $ i => $ match ) { $ matches [ $ i ] = $ matches [ $ i ] [ 0 ] ; } } if ( ! ctype_alnum ( $ substr [ $ offset - 1 ] ) || $ offset == strlen ( $ substr ) || ! ctype_alnum ( $ substr [ $ offset ] ) ) { $ this -> offset += $ offset ; $ ret = array ( ltrim ( $ str ) ) ; } } return $ ret ; }
Matches the string against a regex.

This function matches the string against a regular expressión. If they
match, it advances the offset position and returns an array with a single
string. Otherwise, it returns false. You can use regex without delimiters.
Instead of using /^\s*(\w+)/, you can use simply '\w+'.

Example 1:
```php
// these two lines are identical
if ($t->match("\w+")) doSomething();
if ($t->match("/^\s*(\w+)/")) doSomething();
```

Example 2:
```php
// splits a string into "words"
$t = new TextTokenizer("Lorem ipsum dolor sit amet");
while (list($token) = $t->match("\w+", $matches)) {
echo "$token-";
}
```

Example 3:
```php
// captures the offset
$t = new TextTokenizer("I am 105 years old");
if ($t->match("/\d+/", $matches, TextTokenizer::OFFSET_CAPTURE)) {
print_r($matches);
}
```

Example 4:
```php
// parses a basic SQL sentence
$t = new TextTokenizer("Select Id, Name, Age From users Where Id = 101");
if ($t->match("select")) {
// columns
$columns = array();
while (list($column) = $t->match("\w+")) {
array_push($columns, $column);
if (!$t->match(",")) {
break;
}
}
// `from` clause
if ($t->match("from\s+(\w+)", $matches)) {
$tableName = $matches[1];
echo "You want to get the columns " . implode(", ", $columns) .
" from the table $tableName.";
}
}
```

@param string  $regexp  Regular expression
@param array   $matches Matches (default is array(), passed by reference)
@param integer $flags   Flags (default is 0)

@return false|array of a single string
Matches the string against a regex .