PHP » Strings » strtok()

string strtok(string s, string delimiters)
String to be tokenized.
Characters that can limit the tokens.

Tokenizes a string.

When parsing a string it is often useful to divide it into segments called tokens. strtok() divides a string into tokens at each occurrence of a character in the delimiters parameter. The first time strtok() is used on a string, two arguments should be supplied, the string with tokens and the delimiters. In subsequent calls, only the delimiters argument should be used, since strtok() keeps track of the string and the current position in the string. If strtok() is called with the string again, the tokenization is reset and starts over. For each call, strtok() returns a token, until there are no more tokens.



$email = "John Doe <>";
$delim = " <.@>";

print strtok($email, $delim);
while ($tok = strtok($delim))
   print ", $tok";

John, Doe, john, doe, nowhere, com

This code shows how an email address can be tokenized using the strtok() function.

See Also: