STRTOK(3) 1 STRTOK(3)strtok - Tokenize stringSYNOPSIS
string strtok (string $str, string $token)
DESCRIPTION
string strtok (string $token)
strtok(3) splits a string ($str) into smaller strings (tokens), with each token being delimited by any character from $token. That is, if
you have a string like "This is an example string" you could tokenize this string into its individual words by using the space character as
the token.
Note that only the first call to strtok uses the string argument. Every subsequent call to strtok only needs the token to use, as it keeps
track of where it is in the current string. To start over, or to tokenize a new string you simply call strtok with the string argument
again to initialize it. Note that you may put multiple tokens in the token parameter. The string will be tokenized when any one of the
characters in the argument are found.
PARAMETERS
o $str
- The string being split up into smaller strings (tokens).
o $token
- The delimiter used when splitting up $str.
RETURN VALUES
A string token.
EXAMPLES
Example #1
strtok(3) example
<?php
$string = "This is an example
string";
/* Use tab and newline as tokenizing characters as well */
$tok = strtok($string, "
");
while ($tok !== false) {
echo "Word=$tok<br />";
$tok = strtok("
");
}
?>
The behavior when an empty part was found changed with PHP 4.1.0. The old behavior returned an empty string, while the new, correct,
behavior simply skips the part of the string:
Example #2
Old strtok(3) behavior
<?php
$first_token = strtok('/something', '/');
$second_token = strtok('/');
var_dump($first_token, $second_token);
?>
The above example will output:
string(0) ""
string(9) "something"
Example #3
New strtok(3) behavior
<?php
$first_token = strtok('/something', '/');
$second_token = strtok('/');
var_dump($first_token, $second_token);
?>
The above example will output:
string(9) "something"
bool(false)
NOTES
Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the return value of this function.
SEE ALSO split(3), explode(3).
PHP Documentation Group STRTOK(3)
Check Out this Related Man Page
STRTOK(3) BSD Library Functions Manual STRTOK(3)NAME
strtok, strtok_r -- string tokens
LIBRARY
Standard C Library (libc, -lc)
SYNOPSIS
#include <string.h>
char *
strtok(char *restrict str, const char *restrict sep);
char *
strtok_r(char *restrict str, const char *restrict sep, char **restrict lasts);
DESCRIPTION
This interface is obsoleted by strsep(3).
The strtok() function is used to isolate sequential tokens in a null-terminated string, str. These tokens are separated in the string by at
least one of the characters in sep. The first time that strtok() is called, str should be specified; subsequent calls, wishing to obtain
further tokens from the same string, should pass a null pointer instead. The separator string, sep, must be supplied each time, and may
change between calls.
The implementation will behave as if no library function calls strtok().
The strtok_r() function is a reentrant version of strtok(). The context pointer last must be provided on each call. The strtok_r() function
may also be used to nest two parsing loops within one another, as long as separate context pointers are used.
The strtok() and strtok_r() functions return a pointer to the beginning of each subsequent token in the string, after replacing the token
itself with a NUL character. When no more tokens remain, a null pointer is returned.
EXAMPLES
The following uses strtok_r() to parse two strings using separate contexts:
char test[80], blah[80];
char *sep = "\/:;=-";
char *word, *phrase, *brkt, *brkb;
strcpy(test, "This;is.a:test:of=the/string\tokenizer-function.");
for (word = strtok_r(test, sep, &brkt);
word;
word = strtok_r(NULL, sep, &brkt))
{
strcpy(blah, "blah:blat:blab:blag");
for (phrase = strtok_r(blah, sep, &brkb);
phrase;
phrase = strtok_r(NULL, sep, &brkb))
{
printf("So far we're at %s:%s
", word, phrase);
}
}
SEE ALSO memchr(3), strchr(3), strcspn(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strstr(3), wcstok(3)STANDARDS
The strtok() function conforms to ISO/IEC 9899:1990 (``ISO C90'').
AUTHORS
Wes Peters, Softweyr LLC: <wes@softweyr.com>
Based on the FreeBSD 3.0 implementation.
BUGS
The System V strtok(), if handed a string containing only delimiter characters, will not alter the next starting point, so that a call to
strtok() with a different (or empty) delimiter string may return a non-NULL value. Since this implementation always alters the next starting
point, such a sequence of calls would always return NULL.
BSD November 27, 1998 BSD