Javatpoint Logo
Javatpoint Logo

PHP String strtok() function

PHP strtok() is an in-built function of PHP, which is similar to the C strtok() function. It is used to tokenize or split a string into small parts of string on the basis of given delimiters. It takes input string as an argument along with the delimiters (as a second argument).

The strtok() splits a string into tokens (Small parts of string). E.g., let's take a string such as, "Hlw everyone! Welcome to the wonderland", we can split this string into small individual tokens by using the space character as a token.

One important thing to note about strtok() is that - only the first call of strtok() uses the string argument. In simple words, - It uses string argument only once when we invoke strtok() for the first time. After that, it needs other arguments as the split argument.

Syntax

The syntax for the strtok() is given below which has two arguments, and both are mandatory to be passed.

Parameter

$str (required) - It is the first string parameter which will split into the smaller strings (tokens). It represents the given input string.

$token (required) - This parameter represents the splitting characters. It specifies one or more delimiters or split characters by which string will get split. It is used when splitting up $str.

Return Values

The strtok() returns a string token. It returns the strings separated by the given delimiters, i.e., $token.

Examples

The following are some detailed examples given by which we can understand the working of strtok() properly. Let's check the given examples below-

Example 1

In the below example, we are splitting the string into smaller tokens by taking delimiter as 'e.' It will split the string from, where 'e' will occur.

Output:

The output for the above program will be-

Hi v ryon ! W lcom to javaTpoint

To see the individual tokens, check the html output. The html output for the above program will be shown like the given screenshot below.

PHP String strtok() function

Example 2

In the below example, we are splitting the string into smaller tokens by taking delimiter = " " (space character). It will split the string from, where space will occur.

Output:

The output for the above program will be shown in the browser like this which is given below.

Hlw World! It's a rainy day.

To see the actual tokenize string, right-click on your browser where the output is displayed and select view page source. Here, we can see the individual tokens in html output.

PHP String strtok() function

Note: The strtok() accepts at most two parameters. By providing more than two parameters in strtok() will generate a warning and does not display any output.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA