<?php # originally from http://n...content-available-to-author-only...b.io/2011/10/23/Improving-lexing-performance-in-PHP.html class Lexer { protected $regex; protected $offsetToToken; } public function lex($string) { $offset = 0; } // find the first non-empty element (but skipping $matches[0]) using a quick for loop for ($i = 1; '' === $matches[$i]; ++$i); } return $tokens; } // a recursive function to actually build the structure $current = null; if ($type == T_OPEN) $output[$current] = $this->generate($arr, $i+1); elseif ($type == T_CLOSE) return $output; elseif ($type == T_FIELD) { $output[$element] = null; $current = $element; } } return $output; } } // here begins the magic // this is our $tokenMap '[^,()]+' => T_FIELD, ',' => T_SEPARATOR, '\(' => T_OPEN, '\)' => T_CLOSE ); // this is your string $string = "id,topic,member(name,email,group(id,name)),message(id,title,body)"; $lex = new Lexer($tokenMap); $structure = $lex->lex($string); $output = $lex->generate($structure);
Standard input is empty
Array ( [id] => [topic] => [member] => Array ( [name] => [email] => [group] => Array ( [id] => [name] => ) [id] => ) [name] => [email] => [group] => Array ( [id] => [name] => ) )
PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 54 PHP Notice: Use of undefined constant T_SEPARATOR - assumed 'T_SEPARATOR' in /home/0mdush/prog.php on line 55 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 56 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 58 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38 PHP Notice: Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40 PHP Notice: Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36 PHP Notice: Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38