PHP | strtok() for tokening string
Like C strtok(), PHP strtok() is used to tokenize a string into smaller parts on the basis of given delimiters It takes input String as a argument along with delimiters (as second argument).
Syntax :
string strtok ( string $string, string $delimiters )
Parameters :This function accepts two parameters and both of them are mandatory to be passed.
1. $string: This parameter represents the given input string.
2. $delimiters: This parameter represents the delimiting characters (split characters).
Return Value :This function returns a string or series of strings separated by given delimiters. Series of strings can be found by using strtok() in while loop.
Examples:
Input : $str = "I love GeeksForGeeks"
$delimiters = " "
Output :
I
love
GeeksForGeeks
Input : $str = "Hi,GeeksforGeeks Practice"
$delimiters = ","
Output :
Hi
GeeksforGeeks
Practice
Note that, Only first call needs string argument, after that only delimiters are required because it automatically keeps the status of current string.
Below programs illustrates the strtok() function in PHP:
Program 1 :
<?php // original string $str = "Geeks for Geeks"; // declaring delimiters $del = " "; //calling strtok() function $token = strtok($str, $del); // while loop to get all tokens while ($token !== false) { echo "$token \n"; $token = strtok($del); } ?> |
Geeks for Geeks
Program 2 :
<?php // original string $str = "Hi,GeeksforGeeks Practice"; // declaring delimiters $del = ", "; // calling strtok() function $token = strtok($str, $del); // while loop to get all tokens while ($token !== false) { echo "$token \n"; $token = strtok($del); } ?> |
Hi GeeksforGeeks Practice
Reference : http://php.net/manual/en/function.strtok.php
Recommended Posts:
- JavaScript | Check if a string is a valid JSON string
- JavaScript | Difference between String.slice and String.substring
- PHP | Program to check a string is a rotation of another string
- How to get parameters from a URL string in PHP?
- How to get a variable name as a string in PHP?
- PHP | Different characters in the given string
- PHP | Reverse a String
- PHP | String Functions
- How to get the last n characters of a PHP string?
- TypeScript String
- How to check if URL contain certain string using PHP?
- How to get the last character of a string in JavaScript?
- How to convert string to boolean in PHP?
- PHP to check substring in a string
- How to remove the first character of string in PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



