Re: [PHP-DEV] CVS update: php3/functions From: Shane Caraveo (shane <email protected>)
Date: 09/30/98

Rasmus Lerdorf wrote:
>
> > Fix for bug #800
>
> I don't agree with this fix. You have just made strtok() completely
> non-standard. strtok() has never ignored multiple adjacent tokens, and I
> don't think it should. The C equivalent function certainly doesn't do
> this.
>
> If you have:
>
> $a="a b";
>
> And you tokenize on " " (a space) then you should get:
>
> "a"
> ""
> "b"
>
> when you go through the string. Bug report #800 is bogus because the
> loops relies on the result of the strtok() to be true in order for it to
> continue. If you have two adjacent tokens then you are correctly going to
> get a null string as the strtok() result.
>
> If someone doesn't want this behaviour, then they should compact their
> string before running strtok on it. Something like:
>
> $new = ereg_replace(" +"," ",$string);
>
> Chances are the user should be using explode() anyway though.
>
> -Rasmus

Ok, then M$ has a non-standard strtok function, or are wrong in their
description of the use of strtok, or I completely am misunderstanding
it. I based my 'fix' off of their docs on strtok. (See their example
below)

So in essence, strtok wasn't broken in the first place? Any further
coments before I remove it?

Shane

/* STRTOK.C: In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */

#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

void main( void )
{
   printf( "%s\n\nTokens:\n", string );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}
Output

A string of ,,tokens
and some more tokens

Tokens:
 A
 string
 of
 tokens
 and
 some
 more
 tokens

--
PHP Development Mailing List   http://www.php.net/
To unsubscribe send an empty message to php-dev-unsubscribe <email protected>
For help: php-dev-help <email protected>