By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,770 Members | 1,382 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,770 IT Pros & Developers. It's quick & easy.

Tokenizing a token

P: n/a
I'm writing a snippet of code intended to parse a grid that is
represented by a long string. Each row of the grid is deliminated in
the string with a '~' character. Inside each row substring, columns are
deliminated with a '|' character. I'm therfore trying to use nested
strtok() calls as follows:
$this->grid = array();
$rowTokenizer = strtok($gridString, "~");
$rowIndex = 0;
while($rowTokenizer) {
$colIndex = 0;
$columnTokenizer = strtok($rowTokenizer, "|");
while($columnTokenizer) {
$this->grid[rowIndex][colIndex] = $columnTokenizer;
$colIndex++;
$columnTokenizer = strtok("|");
}
$rowIndex++;
$rowTokenizer = strtok("~");
}
However, this code does not generate a proper grid. I simplifed the
operation just to pring out each row-substring so I could see what's
going on inside:
$rowTokenizer = strtok($gridString, "~");
while($rowTokenizer) {
echo $rowTokenizer . "<br/>";
$columnTokenizer = strtok($rowTokenizer, "|");
while($columnTokenizer) {
$columnTokenizer = strtok("|");
}
$rowTokenizer = strtok("~");
}
... and here's what the output looks like:
#|1|#|#|#|#|2|#|#|#|#|3|#|#
1|#|#|#|#|2|#|#|#|#|3|#|#
#|#|#|#|2|#|#|#|#|3|#|#
#|#|#|2|#|#|#|#|3|#|#
#|#|2|#|#|#|#|3|#|#
#|2|#|#|#|#|3|#|#
2|#|#|#|#|3|#|#
#|#|#|#|3|#|#
#|#|#|3|#|#
#|#|3|#|#
#|3|#|#
3|#|#
#|#
#
It's never making it past the first row. For each interation of the
outer while() loop, $rowTokenizer is simply representing the first row
with the left-most column token stripped out. Can anyone spot what I'm
doing wrong, and how I can sucessfully tokenize a token without
interfering with the outermost tokenization process? Thanks in advance!
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
How about:

$someString; //the input string

$someArray = explode('~', $someString); //$someArray now has the 'rows'

$numrows = count($someArray);
for($i = 0; $i < $numrows; $i++)
$someArray[$i] = explode('|', $someArray[$i]);
//$someArray is now an array of arrays

Jul 17 '05 #2

P: n/a
ZeldorBlat wrote:
How about:

$someString; //the input string

$someArray = explode('~', $someString); //$someArray now has the 'rows'

$numrows = count($someArray);
for($i = 0; $i < $numrows; $i++)
$someArray[$i] = explode('|', $someArray[$i]);
//$someArray is now an array of arrays

Thanks a ton, that fixed my problem! I'd still love to understand the
problem better though, I get the feeling that my understanding of PHP
string tokenization isn't as strong as it should be.

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.