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! 2 2095
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Simon Morgan |
last post by:
I'm trying to write a function to parse a Reverse Polish Notation string
from stdin and return 1 token at a time. For those of you who are unaware
an RPN string looks like this:
1 2 + 4 * 3 +
...
|
by: Manuel |
last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using
devc++.
So I've imported the glut32.dsp into devc++, included manually some
headers, and start to compile.
It return a very...
|
by: wreckingcru |
last post by:
I'm trying to tokenize a C++ string with the following code:
#include <iostream>
#include <stdio>
#include <string>
using namespace std;
int main(int argc, char* argv)
|
by: Joriveek |
last post by:
Hi,
I have a little piece of program here
Basically what it does is, it copies the strings of variable widths. The
basis is until it finds a comma ",". The input is a CSV/Comma Separated...
|
by: Acolyte |
last post by:
Ok, the program I'm working on now involves taking an input string and tokenizing it, by seperating it by spaces. Here's what I've got:
#include <stdio.h>
#include <string.h>
int main (void)
{...
| |
by: arnaudk |
last post by:
Hello,
I need to get the following string
"12332321213,SomeText,3.141592654"
into constituent variables: an unsigned long int, a string and a double. So far I have,
string line =...
|
by: dhirenved |
last post by:
hi, i have to use string tokenizing to isolate commands and the arguments that i have to execute using the execvp command.
here is what i'm using
char* token;
char buf;
printf("enter command");...
|
by: Jonathan Sion |
last post by:
Hi,
I got a large text string, and a bunch of regular expression patterns
i need to find within the string. in other words, to find all the
'tokens' inside it. of course I could use the regexp...
|
by: Linarexol |
last post by:
just a quick question,
does anyone know how to write a regexp for tokenization??????????? to get all email addresses and abbreviations as 1 token?
thanks heaps...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |