2019-02-07 18:43:58 +01:00
|
|
|
class StringTokenizer {
|
2019-03-17 19:48:52 +01:00
|
|
|
private string $token;
|
|
|
|
private string $delim;
|
2019-02-07 18:43:58 +01:00
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
public void __construct(string $str, string $delim = ' ') {
|
2019-02-07 18:43:58 +01:00
|
|
|
$this->token = strtok($str, $delim);
|
|
|
|
$this->delim = $delim;
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
public void __destruct() {
|
2019-02-07 18:43:58 +01:00
|
|
|
unset($this);
|
|
|
|
}
|
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
public bool hasMoreTokens() {
|
2019-03-30 19:52:51 +01:00
|
|
|
return ($this->token != NULL);
|
2019-02-07 18:43:58 +01:00
|
|
|
}
|
|
|
|
|
2019-03-17 19:48:52 +01:00
|
|
|
public string nextToken() {
|
2019-02-07 18:43:58 +01:00
|
|
|
string $current = $this->token;
|
|
|
|
$this->token = strtok($this->delim);
|
|
|
|
return $current;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Program {
|
2019-03-17 19:48:52 +01:00
|
|
|
void main() {
|
2019-02-07 18:43:58 +01:00
|
|
|
string $str = "This is:@\t\n a TEST!";
|
|
|
|
string $delim = " !@:\t\n";
|
|
|
|
object $st = new StringTokenizer($str, $delim);
|
|
|
|
while ($st->hasMoreTokens()) {
|
|
|
|
print($st->nextToken(), "\n");
|
|
|
|
}
|
|
|
|
unset($st);
|
|
|
|
}
|
|
|
|
}
|