English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

PHP strtok() Function Usage and Example

PHP String Manual of String Functions

The strtok() function is used to mark and split strings into smaller strings (markers).

Syntax

strtok(string,split)

Definition and Usage

 strtok() splits the string string into several substrings, each substring is split by the character in token. This means that if there is a string such as "This is an example string", you can use the space character to split this sentence into separate words.
Note that the string parameter should only be used for the first call to the strtok function. For subsequent calls, only the token parameter will be used, as it will remember its position in the string string. If you want to start splitting a new string, you need to use the string parameter again to call strtok to complete the initialization work. Note that multiple characters can be used in the token parameter. The string will be split by any character in the parameter.

Return Value

It returns a marked string.

Parameter

Serial NumberParameters and Descriptions
1

string

String to be split

2

split

Specify one or more split characters

Online Example

Try the following example, splitting strings by spaces:

<?php
   //strtok() function, splits strings by spaces
   $input = "How to learn PHP well? \";
   $token = strtok($input, "\t");
   
   while ($token !== false){
      echo "\$token<br>";
      $token = strtok("\t");
   }
?>
Test and See‹/›

Output Result

How
to
learn
PHP
well?

PHP String Manual of String Functions