strtok()
The strtok() function in PHP is used to break a string into smaller parts, known as tokens. It works by taking a string and breaking it up into smaller parts,
Introduction
The strtok() function in PHP breaks a string into smaller pieces called tokens. A token is a chunk of text between delimiters — single characters such as a space, comma, or newline that mark where one piece ends and the next begins.
What makes strtok() unusual is that it is stateful: it remembers its position in the string between calls. You read the first token, then keep calling it to pull the next token, and the next, until the string is exhausted. This page covers the syntax, how that internal pointer works, the common gotchas, and when you should reach for explode() instead.
Syntax
strtok(string $string, string $delimiter): string|false
// continuation call:
strtok(string $delimiter): string|falseThere are two ways to call it:
| Form | Meaning |
|---|---|
strtok($string, $delimiter) | Start tokenizing $string, return the first token. |
strtok($delimiter) | Continue from where the previous call stopped, return the next token. |
Key points about the parameters:
$delimiteris a set of characters, not a multi-character separator." !?"means "split on a space, an exclamation mark, or a question mark" — each character is its own delimiter. It does not split on the literal three-character string" !?".- The delimiter can change between calls, which is the main reason to use
strtok()overexplode(). - The function returns
falsewhen there are no more tokens (or if$delimiteris empty), so it can be used as a loop condition.
In PHP 8.1+ the old continuation form
strtok(null, $delimiter)is deprecated. Use the single-argument formstrtok($delimiter)to continue.
Basic example: looping through tokens
The typical pattern is one call to start, then a while loop that keeps pulling tokens until strtok() returns false:
<?php
$string = "Hello World! How are you?";
$delimiter = " !?";
$token = strtok($string, $delimiter); // first token
while ($token !== false) {
echo $token . "\n";
$token = strtok($delimiter); // next token (single-arg form)
}Output:
Hello
World
How
are
youUse a strict !== false comparison, not just while ($token). A token of "0" is falsy in PHP, so a loose check would stop early on legitimate data.
Consecutive delimiters are skipped
Unlike explode(), strtok() treats a run of delimiter characters as a single separator and never returns empty tokens:
<?php
$tok = strtok("a,,b", ",");
while ($tok !== false) {
echo "[$tok]\n";
$tok = strtok(",");
}Output:
[a]
[b]The empty piece between the two commas is silently dropped. explode(",", "a,,b") would instead return ["a", "", "b"]. If preserving empty fields matters (CSV, for instance), do not use strtok().
Changing the delimiter mid-parse
Because the internal pointer is preserved, you can switch delimiters between calls — handy for parsing key=value style data:
<?php
$line = "name=John; age=30";
$key = strtok($line, "="); // split on "=" → "name"
$value = strtok(";"); // now split on ";" → " John"
echo trim($key) . "\n";
echo trim($value) . "\n";Output:
name
JohnGrabbing just the first line
A quick idiom for reading only the first line of a multi-line string, without building an array of every line:
<?php
$text = "first line\nsecond line\nthird line";
$firstLine = strtok($text, "\n");
echo $firstLine . "\n";Output:
first linestrtok() vs explode()
Both split strings, but they behave differently:
strtok() | explode() | |
|---|---|---|
| Returns | one token per call | a whole array at once |
| Delimiter | a set of single characters | one fixed multi-character string |
| Empty fields | skipped | preserved |
| State | stateful (internal pointer) | stateless |
For most modern code, explode() is easier to reason about and works well with array functions. Reach for strtok() when you need lazy, token-by-token reading or want to change the delimiter partway through. For comma-separated data with quoting, prefer str_getcsv().
Common pitfalls
- The internal pointer is global per string. Calling another function that uses
strtok()in the middle of your loop will corrupt your position. Finish one tokenization before starting another. - Don't pass the source string again on continuation.
strtok($string, $delimiter)restarts from the beginning every time. Continuation must use the single-argument form. - Empty fields vanish. As shown above,
strtok()cannot tell you that a field was blank.
Conclusion
strtok() walks through a string one token at a time, splitting on any character from a delimiter set and keeping an internal pointer between calls. Its stateful nature makes it ideal for lazy parsing and for switching delimiters mid-stream, while its habit of dropping empty fields makes it a poor fit for fixed-column data. When you simply need every piece as an array, explode() is usually the clearer choice; for splitting into fixed-length chunks, see str_split().