PHP Tutorial PHP Advanced PHP Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

PHP - String strtok() Function



The PHP String strtok() function splits a string str into smaller strings (tokens), with each token being delimited by any character from delim. Only the first call to strtok uses the string argument. Every subsequent call to strtok only needs delim to use, as it keeps track of where it is in the current string.

Syntax

strtok(str, delim)

Parameters

str Required. Specify the string to split up into smaller strings (tokens).
delim Required. Specify delimiter used when splitting up string.

Return Value

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

Example:

The example below shows the usage of strtok() function.

<?php
$str = "To be, or not to be, that is the question.";
$delim = ",@# ";

//getting the first token
$token = strtok($str, $delim);

//searching for all tokens and displaying it
while($token !== false) {
  echo "$token\n";
  $token = strtok($delim);
}
?>

The output of the above code will be:

To
be
or
not
to
be
that
is
the
question.

❮ PHP String functions

5