note 47986 added to function.token-get-all
| From: | bishop at osu1 dot php dot net | Date: | Wed, 08 Dec 2004 06:58:08 +0000 |
| Subject: | note 47986 added to function.token-get-all | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-81513@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.token-get-all.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47986
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47986&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47986&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47986&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47986&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47986&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47986&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47986&report=yes
Search -- http://master.php.net/manage/user-notes.php