php8.5
Home/ Manual/ strings / functions/ strtok

strtok

PHP function Edit on GitHub ✎

(PHP 4, PHP 5, PHP 7, PHP 8)

Tokenize string

Description

strtok(string $string, string $token): string|false

Alternative signature (not supported with named arguments):

strtok(string $token): string|false

strtok() splits a string (string) 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 token argument is found.

Note

This function behaves slightly different from what one may expect being familiar with explode(). First, a sequence of two or more contiguous token characters in the parsed string is considered to be a single delimiter. Also, a token situated at the start or end of the string is ignored. For example, if a string ";aaa;;bbb;" is used, successive calls to strtok() with ";" as a token would return strings "aaa" and "bbb", and then false. As a result, the string will be split into only two elements, while explode(";", $string) would return an array of 5 elements.

Parameters

string

The string being split up into smaller strings (tokens).

token

The delimiter used when splitting up string.

Return Values

A string token, or false if no more tokens are available.

Changelog

VersionDescription
8.3.0Now emits E_WARNING when token is not provided.

Examples

strtok() example

php
<?php
$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well  */
$tok = strtok($string, " \n\t");

while ($tok !== false) {
    echo "Word={$tok}\n";
    $tok = strtok(" \n\t");
}
?>

strtok() behavior on empty part found

php
<?php
$first_token  = strtok('/something', '/');
$second_token = strtok('/');
var_dump($first_token, $second_token);
?>

The above example will output:

output
string(9) "something"
bool(false)

The difference between strtok() and explode()

php
<?php
$string = ";aaa;;bbb;";

$parts = [];
$tok = strtok($string, ";");
while ($tok !== false) {
    $parts[] = $tok;
    $tok = strtok(";");
}
echo json_encode($parts),"\n";

$parts = explode(";", $string);
echo json_encode($parts),"\n";

The above example will output:

output
["aaa","bbb"]
["","aaa","","bbb",""]

Notes Falseproblem

See Also

Source: reference/strings/functions/strtok.xml · from the official PHP manual (php/doc-en)