Updated April 3, 2023
Introduction to PHP strtok()
Just like the other programming languages, PHP strtok() function of the PHP Programming Language helps in tokenizing the string into many smaller parts on some given delimeters basis. It helps in taking the input string as an argument along with the help of the delimiters which is nothing but as the second argument. PHP strtok() function is actually an inbuilt function and it is very much similar to the C strtok() function. The strtok() function helps in splitting a string into many tokens (smaller parts of the string) by taking the space character into consideration if it is necessary.
Syntax:
Strtok(string $str1, string $token1/$delimeter1)
Explanation of the Parameters:
The strtok() function of the PHP coding language usually accepts only two parameters and both of those are mandatory and are need to be passed. One parameter is $str1 parameter and the second one is $token1/$delimeter1 parameter.
- $str1: The $str1 parameter of the strtok() function of the PHP Programming Language helps in representing the input string which is actually provided. It is the actual given string value. This strtok() parameter is a mandatory parameter.
- $token1/$delimeter1: The $token1/$delimeter1 parameter of the strtok() function of the PHP Programming Language helps in representing the delimiting characters (split characters or token characters). This strtok() parameter is also a mandatory parameter.
Return value of the strtok() function:
The strtok() function actually returns a string or group of strings which are separated with the help of the delimeters/tokens. These series/groups of string will be found with the help of the strtok() function usage in the while loop.
How strtok() Function works in PHP?
- The strtok() function of the PHP Programming Language works based on splitting the normal string into many smaller strings with the help of the only two parameters which are actually mandatory and need to be passed.
- One is the string parameter and the second one is the delimeter/token parameter.
- Delimeter parameter helps in splitting the string value into many smaller string elements.
- String value can either be any alphabet or any character or space or anything like that.
- String will be split only at the exact point of the specified delimeter1 parameter value.
- The delimeter1 parameter can have different types of characters, alphabets, numerical values which helps to split the string into small strings from a single big sentence.
Examples of PHP strtok()
Given below are the examples:
Example #1
This is the example of implementing strtok() function with the help of “space” value as $delimeter1 parameter and “Pavan Kumar Sake” as the $str1 parameter’s value. At first, $str1 variable is created with a string value “Pavan Kumar Sake” then $delimeter variable is created with “space”. Then $token1 variable is created with strtok() function along with its two parameters. Then while() function is created with the condition (token1 value is not a FALSE value). If the condition is TRUE then echo statement will print $token1 variable value by splitting the string at the space element’s exact point.
Syntax:
<?php
$str1 = "Pavan Kumar Sake";
$delimeter1 = " ";
$token1 = strtok($str1, $delimeter1);
while ($token1 !== false)
{
echo "$token1 <br>";
$token1 = strtok($delimeter1);
}
?>
Output:
Example #2
This is the example of implementing strtok() function with the help of “,” special character value as $delimeter11 parameter and “Hi,Pavan Kumar Sake Blogger” as the $str11 parameter’s value. At first, $str11 variable is created with a string value “Hi,Pavan Kumar Sake Blogger” then $delimeter11 variable is created with “,” special character. Then $token11 variable is created with strtok() function along with its two parameters. Then while() function is created with the condition (token11 value is not a FALSE value). If the condition is TRUE then echo statement will print $token11 variable value by splitting the string at the “,” special character element’s exact point.
Syntax:
<?php
$str11 = "Hi,PavanKumarSake Blogger";
$delimeter11 = ",";
$token11 = strtok($str11, $delimeter11);
while ($token11 !== false)
{
echo "$token11 <br>";
$token11 = strtok($delimeter11);
}
?>
Output:
Example #3
This is the example of implementing strtok() function of PHP with the help of “space” value as $delimeter11 parameter and “Hellow World! Its?SakePavan Kumar” as the $string11 parameter’s value. At first, $string11 variable is created with a string value “Hellow World! Its?SakePavan Kumar Sake” then $delimeter11 variable is created with “space” element. Then $token111 variable is created with strtok() function along with its two parameters. Then while() function inside of PHP tag is created with the condition (token111 value is not a FALSE value). If the condition inside while loop is TRUE then echo statement will print $token111 variable value by splitting the given/provided string at the space element’s exact point. You can check the output below to know how the splitting of the string sentence into smaller string elements.
Syntax:
<?php
$string11 = "Hellow World! Its?SakePavan Kumar.";
$delimiter11 = " ";
$token111 = strtok($string11, $delimiter11);
while($token111 !==false)
{
echo $token111. "<br>";
$token111 =strtok($delimiter11);
}
?>
Output:
Example #4
This is the example of implementing strtok() function of PHP coding language with the help of “e” alphabet value as $delimeter1 parameter ($del11 variable value) and “Hey Buddy! Welcome to PavanKumarSake Park” as the $str111 parameter’s value. At first, $str111 variable is created with a string value “Hey Buddy! Welcome to PavanKumarSake Park” then $del11 variable is created with “e” alphabet character value. Then $token11 variable is created with strtok() function along with its two parameters($str111 and $del11 parameters). Then while() function is created with the condition ($token11 value is not a FALSE value). If the condition of the loop is TRUE then echo statement of the php will print $token11 variable value by splitting the string at the “e” alphabet characters element’s exact point.
Syntax:
<?php
$str111 = "Hey Buddy! Welcome to PavanKumarSake Park";
$del11 = "e";
$token11 = strtok($str111, $del11);
while($token11 !==false)
{
echo $token11. "<br>";
$token11=strtok($del11);
}
?>
Output:
Conclusion
Here we saw what is the definition of strtok() function of the PHP Programming Language along with its syntax and various parameters explanation, how strtok() function works in PHP along with various examples of implementation to know how strtok() function works.
Recommended Articles
This is a guide to PHP strtok(). Here we discuss the introduction, syntax, and working of strtok() in PHP along with different examples and code implementation. You may also have a look at the following articles to learn more –