3v4l.org

run code in 300+ PHP versions simultaneously
<?php class tokenstream { // Входной поток private $input_stream; // Указатель позиции во входном потоке public $seek = 0; // Конструктор public function __construct ( $input ) { // Инициализируем входной поток $this -> input_stream = $input; } // Возвращает очередной символ из входного потока public function readChar ( ) { $char = ( isset ( $this -> input_stream [ $this -> seek ] ) ? $this -> input_stream [ $this -> seek ] : false ); $this -> seek++; return $char; } // Производит токенизацию public function tokenize ( ) { $output = array ( ); while ( ( $char = $this -> readChar ( ) ) !== false ) { if ( preg_match ( '/\s/', $char ) ) { // Неотображаемые символы } elseif ( preg_match ( '/\"/', $char ) ) { // Кавычки $output [ ] = 'T_QUOTE'; } elseif ( условие ) { $output [ ] = 'T_...'; } } return $output; } } // Исходный код для разбора $my_source = ' function foo ( ) { $my_var = "foo-bar"; } '; // Создаем экземпляр класса парсера $tokenstream = new tokenstream ( $my_source ); // Парсим исходный код и распечатываем результат print_r ( $tokenstream -> tokenize ( ) );

preferences:
69.27 ms | 402 KiB | 5 Q