strtok()

strtok() — Tokenize string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

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.

Signature

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

Parameters

ParameterTypeWhat it takes
$stringstringThe string being split up into smaller strings (tokens).
$tokenstringThe delimiter used when splitting up string.

Return value

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

Example

A runnable snippet:

$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"); }

Changelog

VersionChange
8.3.0Now emits E_WARNING when token is not provided.

See also

explode()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page strtok() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.