Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Tokenizer.class.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * regular expression based tokenizer,
  4. * first token wins.
  5. */
  6. class Tokenizer
  7. {
  8. private $subject;
  9. private $offset = 0;
  10. private $tokens = array(
  11. 'color-fgbg' => '\x03(\d{1,2}),(\d{1,2})',
  12. 'color-fg' => '\x03(\d{1,2})',
  13. 'color-reset' => '\x03',
  14. 'style-bold' => '\x02',
  15. 'catch-all' => '.|\n',
  16. );
  17. public function __construct($subject)
  18. {
  19. $this->subject = (string) $subject;
  20. }
  21. public function setOffset($offset)
  22. {
  23. $this->offset = max(0, $offset);
  24. }
  25. public function getOffset()
  26. {
  27. return $this->offset;
  28. }
  29. /**
  30. * @return array|null
  31. */
  32. public function getNext()
  33. {
  34. if ($this->offset >= strlen($this->subject))
  35. return NULL;
  36. foreach($this->tokens as $name => $token)
  37. {
  38. if (FALSE === $r = preg_match("~$token~", $this->subject, $matches, PREG_OFFSET_CAPTURE, $this->offset))
  39. throw new RuntimeException('Pattern for token %s failed (regex error).', $name);
  40. if ($r === 0)
  41. continue;
  42. if (!isset($matches[0])) {
  43. var_dump(substr($this->subject, $this->offset));
  44. $c = 1;
  45. }
  46. if ($matches[0][1] !== $this->offset)
  47. continue;
  48. $data = array();
  49. foreach($matches as $match)
  50. {
  51. list($data[]) = $match;
  52. }
  53. $this->offset += strlen($data[0]);
  54. return array($name, $data);
  55. }
  56. return NULL;
  57. }
  58. }
  59. ?>