note 47986 deleted from function.token-get-all by googleguy

From: Date: Tue, 22 Oct 2013 10:13:15 +0000
Subject: note 47986 deleted from function.token-get-all by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-196905@lists.php.net to get a copy of this message
Note Submitter: bishop ---- You may want to know the line and column number at which a token begins (or ends). Since this tokenizer interface doesn't provide that information, you have to track it manually, like below: <?php function update_line_and_column_positions($c, &$line, &$col) { // update line count $numNewLines = substr_count($c, "\n"); if (1 <= $numNewLines) { // have new lines, add them in $line += $numNewLines; $col = 1; // skip to right past the last new line, as it won't affect the column position $c = substr($c, strrpos($c, "\n") + 1); if ($c === false) { $c = ''; } } // update column count $col += strlen($c); } ?> Now use it, something like: <?php $line = 1; $col = 1; foreach ($tokens as $token) { if (is_array($token)) { list ($token, $text) = $token; } else if (is_string($token)) { $text = $token; } update_line_and_column_positions($text, $line, $col); } ?> Note this assumes that your desired coordinate system is 1-based (eg (1,1) is the upper left). Zero-based is left as an exercise for the reader.

« previous php.notes (#196905) next »