Page 1 of 1

PHP Tokenizer?

Posted: Thu Apr 13, 2006 6:40 pm
by TreyAU21
Is there a tokenizer function (similar in nature to Java's) where you can parse a string based on a non-space delimter... for instance:

if you have the string "0001;4;hello"

You could parse based on the semicolon into 3 tokens... "0001", "4", and "hello"? If so, how?

THanks,
Trey

Posted: Thu Apr 13, 2006 7:11 pm
by s.dot

Posted: Thu Apr 13, 2006 10:11 pm
by TreyAU21
I found it...

Code: Select all

$tok = strtok($theString, ";");
That will do it.

Posted: Fri Apr 14, 2006 9:54 am
by Ambush Commander
I understand that strtok is the most similar to Java's, but it has some problems (notably the fact that there's only one stack). I prefer tokenizing my strings all at once:

Code: Select all

$string = '0001;4;hello';
$array = explode(';', $string);
foreach ($array as $value) {
  // do stuff
}

Posted: Fri Apr 14, 2006 10:37 am
by timvw
With a (java) tokenizer you can call nextToken("delim") in a loop ... With explode you always have to use the same "delim".