PHP strtok Function | Tokenize a String on Delimiter

PHP strtok is an inbuilt function in PHP. It tokenizes a string into smaller parts according to the input delimiter values. It takes a string and the delimiters as input and returns a series of strings. In this article, we will discuss the PHP strtok Function. Also, we will discuss a few examples of using it.

Syntax

strtok ($string, $delimiter)

strtok ($delimiter)

Parameters

The PHP strtok Function expects two mandatory parameters. The description of the parameters is as follows:

  • $string: The string to split in smaller tokens is the first parameter.
  • $delimiter: The delimiter to use while splitting is the second parameter.

Note: The first parameter is mandatory only in the first call to the function. It keeps track of the current string on subsequent function calls to the function. However, in order to tokenize a different string, you need to initialize the function again with a fresh call by passing the $string parameter. Also, the function supports multiple delimiters. It will tokenize the string when any one of the characters is found.

Return Value

The PHP strtok Function returns a series of strings after tokenizing it on the delimiters. You can retrieve the strings in the series by passing them in the while loop.

Examples

Let’s discuss a few examples of using PHP strtok.

Example 1: Simple Tokenization on Comma

For example, consider a simple tokenizer on comma in the string.

<?php

	$inputString = 'Apple,Banana,Mango';
	$token = strtok($inputString, ',');
	while($token != false) {
		echo $token.PHP_EOL;
		$token = strtok(',');
	}

?>

In the above example, the function splits the string on comma as the delimiter. On looping the output of the function we can retrieve all the tokens from the string. Also, the function keeps track of the original string on calling it subsequently inside the loop. The output of the function is as follows:

OUTPUT:
Apple
Banana
Mango

Example 2: Multiple Delimiters

Similarly, we can also pass multiple delimiter characters in the second parameter. The function will tokenize on encountering any one of the delimiter characters.

<?php

	$inputString = 'Apple,Banana Mango'; //One comma is replaced with a space
	$token = strtok($inputString, ', '); //The delimiter also has a space character
	while($token != false) {
		echo $token.PHP_EOL;
		$token = strtok(', ');
	}

?>

In the above example, we add a space in the list of delimiter characters. Also, we replace one comma with a space in the original string. The PHP strtok Function will split the string if it encounters a space or a comma.

OUTPUT:
Apple
Banana
Mango

Example 3: Passing Different Tokens

Let’s observe what happens on passing different tokens.

<?php

	$inputString = 'Apple,Banana Mango,Grapes';
	$token = strtok($inputString, ' ');
	while($token != false) {
		echo $token.PHP_EOL;
		$token = strtok(',');
	}

?>

In the above example, the PHP strtok Function first tokenizes on a space. Later, it tokenizes the remaining string on a comma. The output of the script will be as follows:

Apple,Banana
Mango
Grapes

Explaination

PHP strtok Function

Conclusion

In conclusion, we discussed the PHP strtok Function. You can read more about it on the Official Documentation of PHP. Additionally, you can learn about more PHP String Functions on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *