strtok
strtok
Tokenize string
**strtok** string $string string $token
Alternative signature (not supported with named arguments):
**strtok** string $token
splits a string ()
into smaller strings (tokens), with each token being delimited by any
character from .
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 .
strtok``string``token``token
Note that only the first call to strtok uses the argument.
Every subsequent call to strtok only needs the 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
argument again to initialize it. Note that you may put
multiple tokens in the parameter. The string will be
tokenized when any one of the characters in the argument is
found.
string``token``string``token``token
Opmerking: > This function behaves slightly different from what one may expect being familiar with . First, a sequence of two or more contiguous characters in the parsed string is considered to be a single delimiter. Also, a situated at the start or end of the string is ignored. For example, if a string is used, successive calls to with as a would return strings "aaa" and "bbb", and then false. As a result, the string will be split into only two elements, while would return an array of 5 elements.
explode``token``token``";aaa;;bbb;"``strtok``";"``token``explode(";", $string)
string
The being split up into smaller strings (tokens).
string
token
The delimiter used when splitting up .
string
A token, or false if no more tokens are available.
string
Voorbeeld: example
<?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");
}
?>
Voorbeeld: behavior on empty part found
<?php
$first_token = strtok('/something', '/');
$second_token = strtok('/');
var_dump($first_token, $second_token);
?>
string(9) "something"
bool(false)
**Voorbeeld: The difference between and **
<?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";
["aaa","bbb"]
["","aaa","","bbb",""]
explode