strtok

Tokenize string

Syntax

strtok ( string $str , string $token ) : string
strtok ( string $token ) : string

Parameters

str

The string being split up into smaller strings (tokens).

token

The delimiter used when splitting up str.

Return

A string token.

Examples

1

<?

$str = "string1\nstring2";
$token = "\n";

$return = strtok($str, $token);

echo $return;

?>
string1

2

<?

$str = "string1\nstring2\tstring3 string4";
$token = "\n\t ";

$return = strtok($str, $token);

echo $return;

?>
string1

3

<?

$token = "\n\t ";

$return = strtok($token);

var_dump($return);

?>
bool(false)

4

<?

$str = "string1\nstring2\tstring3 string4";
$token = "\n\t ";

$return = strtok($str, $token);

while ($return !== false) {
    echo "$return<br>";
    $return = strtok($token);
}

?>
string1<br>string2<br>string3<br>string4<br>

PHP

Home Menu