The Tokenizer class is a small class that allows you to split a string into smaller pieces. Unlike other classes, it is based on regular patterns. Thanks to this, this class lets you to parse a string on the fly. For example:
<?php
// parses a basic SQL sentence
$t = new Tokenizer("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)) {
$table_name = $matches[1];
echo "You want to get the columns " . implode(", ", $columns) . " from the table $table_name.\n";
}
}
?>
|