PHP Tokenizer?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TreyAU21
Forum Newbie
Posts: 9
Joined: Thu Apr 13, 2006 6:37 pm
Location: Woodstock, GA

PHP Tokenizer?

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
TreyAU21
Forum Newbie
Posts: 9
Joined: Thu Apr 13, 2006 6:37 pm
Location: Woodstock, GA

Post by TreyAU21 »

I found it...

Code: Select all

$tok = strtok($theString, ";");
That will do it.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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
}
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

With a (java) tokenizer you can call nextToken("delim") in a loop ... With explode you always have to use the same "delim".
Post Reply