PHP strtok() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strtok() splits a string into tokens (pieces) one at a time using a set of delimiter characters.

The first call gets the string and delimiters. Later calls pass only the delimiters and return the next token.

Syntax

Call strtok() with the values you need. Start with this line.

PHP
$tok = strtok($text, " ");
CallWhat it does
strtok("red green blue", " ")Starts tokenizing and returns "red".
strtok(" ")Returns the next token, such as "green".
strtok(" ")Returns false when no tokens are left.

PHP also has a second form, strtok(string $token): string|false, used for every call after the first one.

Library declarations
PHP
strtok(string $string, string $token): string|false

Parameters

ParameterDescription
stringRequired on the first call. The string to split. Leave it out on later calls.
tokenRequired. A set of delimiter characters. Each character is a separate delimiter.

Return Value

Return ValueDescription
stringThe next token.
falseReturned when there are no more tokens.

PHP Examples

Example 1 — Loop Through Words

The classic pattern: call once with the string, then keep calling with the delimiter.

PHP
<?php
$text = "red green blue";

$tok = strtok($text, " ");
while ($tok !== false) {
    echo $tok, PHP_EOL;
    $tok = strtok(" ");
}

How It Works

  1. Start tokenizing. strtok($text, " ") returns the first word.
  2. Check for false. The loop runs while a token exists.
  3. Ask for the next token. strtok(" ") needs no string this time.
  4. Stop at the end. When pieces run out, strtok() returns false.

Example 2 — Skipped Empty Tokens

Two commas in a row do not create an empty token.

PHP
<?php
$first  = strtok("a,,b", ",");
$second = strtok(",");
$third  = strtok(",");

echo $first, PHP_EOL;
echo $second, PHP_EOL;
var_dump($third);

How It Works

  1. First token. It returns "a".
  2. Double comma. The empty piece between the commas is skipped.
  3. Second token. It returns "b".
  4. Nothing left. The third call returns false. Use explode() if you need empty pieces.

Example 3 — Several Delimiters at Once

Each character in the token string is a delimiter, so one call can split on many characters.

PHP
<?php
$stamp = "2026-11-11 10:30";
$parts = [];

$tok = strtok($stamp, "- :");
while ($tok !== false) {
    $parts[] = $tok;
    $tok = strtok("- :");
}

echo implode(" | ", $parts), PHP_EOL;

How It Works

  1. Set delimiters. "- :" means dash, space, and colon.
  2. Collect tokens. Each token is added to the $parts array.
  3. Keep the same delimiters. Later calls pass the same set.
  4. Join for display. implode() prints all five pieces.

Frequently Asked Questions

strtok() breaks a string into tokens. You call it once with the string and delimiters, then call it again with just the delimiters to get each next token.
string|false strtok(string $string, string $token) for the first call, and string|false strtok(string $token) for later calls.
No. It skips empty tokens caused by consecutive delimiters. Use explode() if you need to keep them.
Yes, but each character is treated as its own delimiter. strtok($s, ", ") splits on commas and on spaces.
explode() splits everything at once into an array using one delimiter string. strtok() returns one token at a time and treats every character in the token argument as a delimiter.

Next: strtolower()

strtolower() converts every letter in a string to lowercase.

strtolower() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful