Jump to content

Archived

This topic is now archived and is closed to further replies.

loclan

How do you go to previous/next token?

Recommended Posts

Ok, this has stumped me for the day (and the day has just started):

$my_string = "This is a test string";
$my_token = strtok ($my_string, " ");

while ($my_token) {
do something...

[b][i]MY QUESTION: let's say, the current $my_token is
now "string". How do I check if the previous $my_token
is actually "test"?[/i][/b]

$my_token = strtok(" ");
}

I know, I could have used the "for loop" to make things simpler - but I didn't ;-)

Thank you.

Share this post


Link to post
Share on other sites
Maybe...?
[code]
$i = 0;
while ($my_token) {
  $tmp[$i] = $my_token;
  if ($my_token == 'string') {
    $previous = $tmp[$i-1];
  }
  $i++;
}
[/code]
A bit messy but hey...

Share this post


Link to post
Share on other sites
[!--quoteo(post=361588:date=Apr 4 2006, 11:00 AM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Apr 4 2006, 11:00 AM) [snapback]361588[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Maybe...?
[code]
$i = 0;
while ($my_token) {
  $tmp[$i] = $my_token;
  if ($my_token == 'string') {
    $previous = $tmp[$i-1];
  }
  $i++;
}
[/code]
A bit messy but hey...
[/quote]

Hmm... yeah, that should work - messy, but should work. If anyone knows a cleaner solution, much appreciated.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.