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
$str = "Geeks for Geeks" ;
$del = " " ;
$token = strtok ( $str , $del );
while ( $token !== false)
{
echo "$token \n" ;
$token = strtok ( $del );
}
?>
|
Program 2 :
<?php
$str = "Hi,GeeksforGeeks Practice" ;
$del = ", " ;
$token = strtok ( $str , $del );
while ( $token !== false)
{
echo "$token \n" ;
$token = strtok ( $del );
}
?>
|
Output:
Hi
GeeksforGeeks
Practice
Reference : http://php.net/manual/en/function.strtok.php